diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..481d591 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "rust-analyzer.linkedProjects": [ + "./rust/ares/Cargo.toml", + "./rust/ares_crypto/Cargo.toml" + ], +} diff --git a/rust/ares/Cargo.lock b/rust/ares/Cargo.lock index 7bee7f8..882e98e 100644 --- a/rust/ares/Cargo.lock +++ b/rust/ares/Cargo.lock @@ -2,6 +2,43 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aead" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0" +dependencies = [ + "crypto-common", + "generic-array", +] + +[[package]] +name = "aes" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac1f845298e95f983ff1944b728ae08b8cebab80d684f0a832ed0fc74dfa27e2" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures", +] + +[[package]] +name = "aes-siv" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e08d0cdb774acd1e4dac11478b1a0c0d203134b2aab0ba25eb430de9b18f8b9" +dependencies = [ + "aead", + "aes", + "cipher", + "cmac", + "ctr", + "dbl", + "digest", + "zeroize", +] + [[package]] name = "aho-corasick" version = "1.1.2" @@ -21,6 +58,7 @@ checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" name = "ares" version = "0.1.0" dependencies = [ + "ares_crypto", "ares_macros", "assert_no_alloc", "autotools", @@ -28,7 +66,7 @@ dependencies = [ "cc", "criterion", "either", - "ibig", + "ibig 0.3.6", "intmap", "json", "lazy_static", @@ -42,6 +80,22 @@ dependencies = [ "urcrypt-sys", ] +[[package]] +name = "ares_crypto" +version = "0.1.0" +dependencies = [ + "aes", + "aes-siv", + "assert_no_alloc", + "curve25519-dalek", + "ed25519-dalek", + "ibig 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "sha1", + "sha2", + "urcrypt-sys", + "x25519-dalek", +] + [[package]] name = "ares_macros" version = "0.1.0" @@ -129,6 +183,15 @@ dependencies = [ "wyz", ] +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "bumpalo" version = "3.14.0" @@ -192,6 +255,16 @@ dependencies = [ "half", ] +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", +] + [[package]] name = "clang-sys" version = "1.6.1" @@ -224,6 +297,26 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "cmac" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8543454e3c3f5126effff9cd44d562af4e31fb8ce1cc0d3dcd8f084515dbc1aa" +dependencies = [ + "cipher", + "dbl", + "digest", +] + +[[package]] +name = "cpufeatures" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce420fe07aecd3e67c5f910618fe65e94158f6dcc0adf44e00d69ce2bdfe0fd0" +dependencies = [ + "libc", +] + [[package]] name = "criterion" version = "0.4.0" @@ -293,6 +386,94 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "rand_core", + "typenum", +] + +[[package]] +name = "ctr" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0369ee1ad671834580515889b80f2ea915f23b8be8d0daa4bbaf2ac5c7590835" +dependencies = [ + "cipher", +] + +[[package]] +name = "curve25519-dalek" +version = "4.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e89b8c6a2e4b1f45971ad09761aafb85514a84744b67a95e32c3cc1352d1f65c" +dependencies = [ + "cfg-if", + "cpufeatures", + "curve25519-dalek-derive", + "digest", + "fiat-crypto", + "platforms", + "rustc_version", + "subtle", +] + +[[package]] +name = "curve25519-dalek-derive" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.39", +] + +[[package]] +name = "dbl" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd2735a791158376708f9347fe8faba9667589d82427ef3aed6794a8981de3d9" +dependencies = [ + "generic-array", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", + "subtle", +] + +[[package]] +name = "ed25519" +version = "2.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" +dependencies = [ + "signature", +] + +[[package]] +name = "ed25519-dalek" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f628eaec48bfd21b865dc2950cfa014450c01d2fa2b69a86c2fd5844ec523c0" +dependencies = [ + "curve25519-dalek", + "ed25519", + "sha2", + "subtle", +] + [[package]] name = "either" version = "1.9.0" @@ -301,20 +482,36 @@ checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" [[package]] name = "errno" -version = "0.3.6" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c18ee0ed65a5f1f81cac6b1d213b69c35fa47d4252ad41f1486dbd8226fe36e" +checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" dependencies = [ "libc", "windows-sys", ] +[[package]] +name = "fiat-crypto" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27573eac26f4dd11e2b1916c3fe1baa56407c83c71a773a8ba17ec0bca03b6b7" + [[package]] name = "funty" version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "glob" version = "0.3.1" @@ -344,9 +541,9 @@ dependencies = [ [[package]] name = "home" -version = "0.5.5" +version = "0.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5444c27eef6923071f7ebcc33e3444508466a76f7a2b93da00ed6e19f30c1ddb" +checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" dependencies = [ "windows-sys", ] @@ -361,6 +558,18 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "ibig" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1fcc7f316b2c079dde77564a1360639c1a956a23fa96122732e416cb10717bb" +dependencies = [ + "cfg-if", + "num-traits", + "rand", + "static_assertions", +] + [[package]] name = "indexmap" version = "1.9.3" @@ -371,6 +580,15 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "inout" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5" +dependencies = [ + "generic-array", +] + [[package]] name = "intmap" version = "1.1.0" @@ -437,9 +655,9 @@ dependencies = [ [[package]] name = "linux-raw-sys" -version = "0.4.11" +version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "969488b55f8ac402214f3f5fd243ebb7206cf82de60d3172994707a4bcc2b829" +checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" [[package]] name = "log" @@ -543,6 +761,12 @@ version = "0.3.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" +[[package]] +name = "platforms" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14e6ab3f592e6fb464fc9712d8d6e6912de6473954635fd76a589d832cffcbb0" + [[package]] name = "plotters" version = "0.3.5" @@ -676,10 +900,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" [[package]] -name = "rustix" -version = "0.38.21" +name = "rustc_version" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b426b0506e5d50a7d8dafcf2e81471400deb602392c7dd110815afb4eaf02a3" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + +[[package]] +name = "rustix" +version = "0.38.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72e572a5e8ca657d7366229cdde4bd14c4eb5499a9573d4d366fe1b599daa316" dependencies = [ "bitflags 2.4.1", "errno", @@ -709,6 +942,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "semver" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" + [[package]] name = "serde" version = "1.0.192" @@ -740,6 +979,28 @@ dependencies = [ "serde", ] +[[package]] +name = "sha1" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "shlex" version = "1.2.0" @@ -765,12 +1026,24 @@ dependencies = [ "libc", ] +[[package]] +name = "signature" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" + [[package]] name = "static_assertions" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "subtle" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" + [[package]] name = "syn" version = "1.0.109" @@ -815,6 +1088,12 @@ dependencies = [ "serde_json", ] +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + [[package]] name = "unicode-ident" version = "1.0.12" @@ -831,6 +1110,12 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "walkdir" version = "2.4.0" @@ -950,18 +1235,18 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-sys" -version = "0.48.0" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ "windows-targets", ] [[package]] name = "windows-targets" -version = "0.48.5" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", @@ -974,45 +1259,45 @@ dependencies = [ [[package]] name = "windows_aarch64_gnullvm" -version = "0.48.5" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" +checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" [[package]] name = "windows_aarch64_msvc" -version = "0.48.5" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" +checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" [[package]] name = "windows_i686_gnu" -version = "0.48.5" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" +checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" [[package]] name = "windows_i686_msvc" -version = "0.48.5" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" +checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" [[package]] name = "windows_x86_64_gnu" -version = "0.48.5" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" +checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" [[package]] name = "windows_x86_64_gnullvm" -version = "0.48.5" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" +checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" [[package]] name = "windows_x86_64_msvc" -version = "0.48.5" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" +checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" [[package]] name = "wyz" @@ -1022,3 +1307,19 @@ checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" dependencies = [ "tap", ] + +[[package]] +name = "x25519-dalek" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb66477291e7e8d2b0ff1bcb900bf29489a9692816d79874bea351e7a8b6de96" +dependencies = [ + "curve25519-dalek", + "rand_core", +] + +[[package]] +name = "zeroize" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d" diff --git a/rust/ares/Cargo.toml b/rust/ares/Cargo.toml index 9b56af0..f23e8e1 100644 --- a/rust/ares/Cargo.toml +++ b/rust/ares/Cargo.toml @@ -11,6 +11,7 @@ edition = "2018" # Please keep these alphabetized [dependencies] +ares_crypto = { path = "../ares_crypto" } ares_macros = { path = "../ares_macros" } # assert_no_alloc = "1.1.2" # use this when debugging requires allocation (e.g. eprintln) diff --git a/rust/ares/src/jets/lock/sha.rs b/rust/ares/src/jets/lock/sha.rs index a2d87f3..b769d86 100644 --- a/rust/ares/src/jets/lock/sha.rs +++ b/rust/ares/src/jets/lock/sha.rs @@ -4,6 +4,7 @@ use crate::jets::util::slot; use crate::jets::{JetErr, Result}; use crate::noun::{IndirectAtom, Noun, D}; use urcrypt_sys::*; +use ares_crypto::sha::{ac_sha1, ac_shal, ac_shas, ac_shay}; crate::gdb!(); @@ -11,22 +12,21 @@ pub fn jet_shas(context: &mut Context, subject: Noun) -> Result { let stack = &mut context.stack; let sam = slot(subject, 6)?; let sal = slot(sam, 2)?.as_atom()?; - let ruz = slot(sam, 3)?.as_atom()?; + let mut ruz = slot(sam, 3)?.as_atom()?; let sal_bytes = &(sal.as_bytes())[0..met(3, sal)]; // drop trailing zeros - let (mut _salt_ida, salt) = unsafe { IndirectAtom::new_raw_mut_bytes(stack, sal_bytes.len()) }; + let (mut _salt_ida, mut salt) = unsafe { IndirectAtom::new_raw_mut_bytes(stack, sal_bytes.len()) }; salt.copy_from_slice(sal_bytes); - let message = &(ruz.as_bytes())[0..met(3, ruz)]; // drop trailing zeros + let msg_len = met(3, ruz); + let mut message = &mut (ruz.as_mut_bytes())[0..msg_len]; // drop trailing zeros unsafe { - let (mut out_ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 32); - urcrypt_shas( - salt.as_mut_ptr(), - salt.len(), - message.as_ptr(), - message.len(), - out.as_mut_ptr(), + let (mut out_ida, mut out) = IndirectAtom::new_raw_mut_bytes(stack, 32); + ac_shas( + &mut message, + &mut salt, + &mut out, ); Ok(out_ida.normalize_as_atom().as_noun()) } @@ -35,12 +35,12 @@ pub fn jet_shas(context: &mut Context, subject: Noun) -> Result { pub fn jet_shax(context: &mut Context, subject: Noun) -> Result { let stack = &mut context.stack; let sam = slot(subject, 6)?; - let msg = sam.as_atom()?; + let mut msg = sam.as_atom()?; let len = met(3, msg); unsafe { - let (mut ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 32); - urcrypt_shay(msg.as_bytes().as_ptr(), len, out.as_mut_ptr()); + let (mut ida, mut out) = IndirectAtom::new_raw_mut_bytes(stack, 32); + ac_shay(&mut (msg.as_mut_bytes())[0..len], &mut out); Ok(ida.normalize_as_atom().as_noun()) } } @@ -49,18 +49,18 @@ pub fn jet_shay(context: &mut Context, subject: Noun) -> Result { let stack = &mut context.stack; let sam = slot(subject, 6)?; let wid = slot(sam, 2)?.as_atom()?; - let dat = slot(sam, 3)?.as_atom()?; + let mut dat = slot(sam, 3)?.as_atom()?; let width = match wid.as_direct() { Ok(direct) => direct.data() as usize, Err(_) => return Err(JetErr::Fail(Error::NonDeterministic(D(0)))), }; - let message = dat.as_bytes(); + let message = &mut (dat.as_mut_bytes())[0..width]; unsafe { - let (mut out_ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 32); - urcrypt_shay(message.as_ptr(), width, out.as_mut_ptr()); + let (mut out_ida, mut out) = IndirectAtom::new_raw_mut_bytes(stack, 32); + ac_shay(message, &mut out); Ok(out_ida.normalize_as_atom().as_noun()) } } @@ -69,18 +69,18 @@ pub fn jet_shal(context: &mut Context, subject: Noun) -> Result { let stack = &mut context.stack; let sam = slot(subject, 6)?; let wid = slot(sam, 2)?.as_atom()?; - let dat = slot(sam, 3)?.as_atom()?; + let mut dat = slot(sam, 3)?.as_atom()?; let width = match wid.as_direct() { Ok(direct) => direct.data() as usize, Err(_) => return Err(JetErr::Fail(Error::NonDeterministic(D(0)))), }; - let message = &(dat.as_bytes())[0..met(3, dat)]; // drop trailing zeros + let message = &mut (dat.as_mut_bytes())[0..width]; // drop trailing zeros unsafe { - let (mut ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 64); - urcrypt_shal(message.as_ptr(), width, out.as_mut_ptr()); + let (mut ida, mut out) = IndirectAtom::new_raw_mut_bytes(stack, 64); + ac_shal(message, &mut out); Ok(ida.normalize_as_atom().as_noun()) } } @@ -97,12 +97,12 @@ pub fn jet_sha1(context: &mut Context, subject: Noun) -> Result { }; unsafe { - let msg_bytes = dat.as_bytes(); - let (mut _msg_ida, msg) = IndirectAtom::new_raw_mut_bytes(stack, msg_bytes.len()); + let msg_bytes = &(dat.as_bytes())[0..width]; + let (mut _msg_ida, mut msg) = IndirectAtom::new_raw_mut_bytes(stack, msg_bytes.len()); msg.copy_from_slice(msg_bytes); - let (mut out_ida, out) = IndirectAtom::new_raw_mut_bytes(stack, 20); - urcrypt_sha1(msg.as_mut_ptr(), width, out.as_mut_ptr()); + let (mut out_ida, mut out) = IndirectAtom::new_raw_mut_bytes(stack, 20); + ac_sha1(&mut msg, &mut out); Ok(out_ida.normalize_as_atom().as_noun()) } } diff --git a/rust/ares/src/noun.rs b/rust/ares/src/noun.rs index 4e81db9..72272fd 100644 --- a/rust/ares/src/noun.rs +++ b/rust/ares/src/noun.rs @@ -249,6 +249,11 @@ impl DirectAtom { let bytes: &[u8; 8] = unsafe { std::mem::transmute(&self.0) }; &bytes[..] } + + pub fn as_mut_bytes(&mut self) -> &mut [u8] { + let bytes: &mut [u8; 8] = unsafe { std::mem::transmute(&mut self.0) }; + &mut bytes[..] + } } impl fmt::Display for DirectAtom { @@ -469,6 +474,10 @@ impl IndirectAtom { unsafe { from_raw_parts(self.data_pointer() as *const u8, self.size() << 3) } } + pub fn as_mut_bytes(&mut self) -> &mut [u8] { + unsafe { from_raw_parts_mut(self.data_pointer_mut() as *mut u8, self.size() << 3) } + } + /** BitSlice view on an indirect atom, with lifetime tied to reference to indirect atom. */ pub fn as_bitslice(&self) -> &BitSlice { BitSlice::from_slice(self.as_slice()) @@ -801,6 +810,14 @@ impl Atom { } } + pub fn as_mut_bytes(&mut self) -> &mut [u8] { + if self.is_direct() { + unsafe { self.direct.as_mut_bytes() } + } else { + unsafe { self.indirect.as_mut_bytes() } + } + } + pub fn as_u64(self) -> Result { if self.is_direct() { Ok(unsafe { self.direct.data() }) diff --git a/rust/ares_crypto/src/sha.rs b/rust/ares_crypto/src/sha.rs index 9c8a9a9..724ee54 100644 --- a/rust/ares_crypto/src/sha.rs +++ b/rust/ares_crypto/src/sha.rs @@ -2,7 +2,7 @@ use sha1::{Digest, Sha1}; use sha2::{Sha256, Sha512}; /// Hashes a message using SHA-1. -pub fn ac_sha1(message: &mut [u8], out: &mut [u8; 20]) { +pub fn ac_sha1(message: &mut [u8], out: &mut [u8]) { message.reverse(); let mut hasher = Sha1::new(); hasher.update(message); @@ -12,7 +12,7 @@ pub fn ac_sha1(message: &mut [u8], out: &mut [u8; 20]) { } /// Hashes a message using SHA-256. -pub fn ac_shay(message: &mut [u8], out: &mut [u8; 32]) { +pub fn ac_shay(message: &mut [u8], out: &mut [u8]) { let mut hasher = Sha256::new(); hasher.update(message); let result = hasher.finalize(); @@ -20,7 +20,7 @@ pub fn ac_shay(message: &mut [u8], out: &mut [u8; 32]) { } /// Hashes a message using SHA-512. -pub fn ac_shal(message: &mut [u8], out: &mut [u8; 64]) { +pub fn ac_shal(message: &mut [u8], out: &mut [u8]) { let mut hasher = Sha512::new(); hasher.update(message); let result = hasher.finalize(); @@ -28,7 +28,7 @@ pub fn ac_shal(message: &mut [u8], out: &mut [u8; 64]) { } /// Hashes a message and salt using SHA-256. -pub fn ac_shas(message: &mut [u8], salt: &mut [u8], out: &mut [u8; 32]) { +pub fn ac_shas(message: &mut [u8], salt: &mut [u8], out: &mut [u8]) { let mut mid: [u8; 32] = [0; 32]; ac_shay(message, &mut mid);