diff --git a/Cargo.lock b/Cargo.lock index 4490afc..fdab952 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,20 @@ dependencies = [ "memchr", ] +[[package]] +name = "bit-wrap" +version = "0.1.0" +dependencies = [ + "quote", + "unsynn", +] + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "cfg-if" version = "1.0.3" @@ -24,12 +38,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bba18ee93d577a8428902687bcc2b6b45a56b1981a1f6d779731c86cc4c5db18" [[package]] -name = "des" +name = "des-lib" version = "0.1.0" dependencies = [ + "bit-wrap", "claims", "rand", "rstest", + "thiserror", ] [[package]] @@ -81,6 +97,15 @@ dependencies = [ "slab", ] +[[package]] +name = "fxhash" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" +dependencies = [ + "byteorder", +] + [[package]] name = "getrandom" version = "0.3.3" @@ -127,6 +152,12 @@ version = "2.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" +[[package]] +name = "mutants" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc0287524726960e07b119cebd01678f852f147742ae0d925e6a520dca956126" + [[package]] name = "pin-project-lite" version = "0.2.16" @@ -309,6 +340,12 @@ dependencies = [ "syn", ] +[[package]] +name = "shadow_counted" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65da48d447333cebe1aadbdd3662f3ba56e76e67f53bc46f3dd5f67c74629d6b" + [[package]] name = "slab" version = "0.4.11" @@ -326,6 +363,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "2.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f63587ca0f12b72a0600bcba1d40081f830876000bb46dd2337a3051618f4fc8" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ff15c8ecd7de3849db632e14d18d2571fa09dfc5ed93479bc4485c7a517c913" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "toml_datetime" version = "0.7.2" @@ -362,6 +419,18 @@ version = "1.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d" +[[package]] +name = "unsynn" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7940603a9e25cf11211cc43b81f4fcad2b8ab4df291ca855f32c40e1ac22d5bc" +dependencies = [ + "fxhash", + "mutants", + "proc-macro2", + "shadow_counted", +] + [[package]] name = "wasi" version = "0.14.7+wasi-0.2.4" diff --git a/Cargo.toml b/Cargo.toml index 3bf535b..a2d127f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,17 +1,15 @@ -[package] -name = "des" -version = "0.1.0" -authors = ["Kristofers Solo "] -edition = "2024" +[workspace] +resolver = "2" +members = ["des-lib", "bit-wrap"] -[dependencies] - -[dev-dependencies] +[workspace.dependencies] +bit-wrap = { path = "bit-wrap" } +thiserror = "2" claims = "0.8" rand = "0.9" rstest = "0.26" -[lints.clippy] +[workspace.lints.clippy] pedantic = "warn" nursery = "warn" unwrap_used = "warn" diff --git a/des-lib/Cargo.toml b/des-lib/Cargo.toml new file mode 100644 index 0000000..36e1ed4 --- /dev/null +++ b/des-lib/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "des-lib" +version = "0.1.0" +authors = ["Kristofers Solo "] +edition = "2024" + +[dependencies] +bit-wrap.workspace = true +thiserror.workspace = true + +[dev-dependencies] +claims.workspace = true +rand.workspace = true +rstest.workspace = true + +[lints] +workspace = true diff --git a/des-lib/src/blocks/mod.rs b/des-lib/src/blocks/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/constants.rs b/des-lib/src/constants.rs similarity index 100% rename from src/constants.rs rename to des-lib/src/constants.rs diff --git a/des-lib/src/error.rs b/des-lib/src/error.rs new file mode 100644 index 0000000..e69de29 diff --git a/des-lib/src/keys/key.rs b/des-lib/src/keys/key.rs new file mode 100644 index 0000000..3c16686 --- /dev/null +++ b/des-lib/src/keys/key.rs @@ -0,0 +1,29 @@ +use std::fmt::{Debug, Display}; + +#[derive(Clone, Copy, PartialEq, Eq)] +pub struct Key(u64); + +impl Key { + // #[macro_use] + // pub fn new(key: u64) -> Self { + // key.into() + // } +} + +impl From for Key { + fn from(key: u64) -> Self { + Self(key) + } +} + +impl Debug for Key { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "Key(0x{:016X})", self.0) + } +} + +impl Display for Key { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "0x{:016X}", self.0) + } +} diff --git a/des-lib/src/keys/mod.rs b/des-lib/src/keys/mod.rs new file mode 100644 index 0000000..2d7bd0c --- /dev/null +++ b/des-lib/src/keys/mod.rs @@ -0,0 +1,5 @@ +mod key; +mod subkey; + +pub use key::Key; +pub use subkey::Subkey; diff --git a/des-lib/src/keys/subkey.rs b/des-lib/src/keys/subkey.rs new file mode 100644 index 0000000..965b746 --- /dev/null +++ b/des-lib/src/keys/subkey.rs @@ -0,0 +1,33 @@ +use bit_wrap::BitWrapper; + +#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, BitWrapper)] +#[bit_width(48)] +pub struct Subkey(u64); + +// impl TryFrom for Subkey { +// type Error = SubkeyError; +// fn try_from(key: u64) -> Result { +// if key > Self::MAX { +// return Err(SubkeyError::ValueOutOfRange(key)); +// } +// Ok(Self(key)) +// } +// } +// +// impl From for Subkey { +// fn from(value: u32) -> Self { +// Self(u64::from(value)) +// } +// } +// +// impl From for Subkey { +// fn from(value: u16) -> Self { +// Self(u64::from(value)) +// } +// } +// +// impl From for Subkey { +// fn from(value: u8) -> Self { +// Self(u64::from(value)) +// } +// } diff --git a/src/lib.rs b/des-lib/src/lib.rs similarity index 99% rename from src/lib.rs rename to des-lib/src/lib.rs index 3c380ca..c9e445b 100644 --- a/src/lib.rs +++ b/des-lib/src/lib.rs @@ -1,6 +1,10 @@ +mod blocks; mod constants; +mod error; +mod keys; -use crate::constants::{E_BOX, FP, IP, PC1_TABLE, PC2_TABLE, P_BOX, ROUND_ROTATIONS, S_BOXES}; +use crate::constants::{E_BOX, FP, IP, P_BOX, PC1_TABLE, PC2_TABLE, ROUND_ROTATIONS, S_BOXES}; +pub use keys::{Key, Subkey}; #[derive(Debug)] pub struct Des { diff --git a/tests/des.rs b/des-lib/tests/des.rs similarity index 100% rename from tests/des.rs rename to des-lib/tests/des.rs diff --git a/tests/key_schedule.rs b/des-lib/tests/key_schedule.rs similarity index 100% rename from tests/key_schedule.rs rename to des-lib/tests/key_schedule.rs