sha: sha1, shal, shay, shas

This commit is contained in:
Matthew LeVan 2023-12-20 11:13:28 -05:00
parent 0fb271c53a
commit 0f683464a5
4 changed files with 110 additions and 137 deletions

View File

@ -10,7 +10,6 @@ checksum = "d122413f284cf2d62fb1b7db97e02edb8cda96d769b16e443a4f6195e35662b0"
dependencies = [
"crypto-common",
"generic-array",
"heapless",
]
[[package]]
@ -71,15 +70,6 @@ version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55ca83137a482d61d916ceb1eba52a684f98004f18e0cafea230fe5579c178a3"
[[package]]
name = "atomic-polyfill"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4"
dependencies = [
"critical-section",
]
[[package]]
name = "autocfg"
version = "1.1.0"
@ -124,12 +114,6 @@ dependencies = [
"generic-array",
]
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "cexpr"
version = "0.6.0"
@ -177,12 +161,6 @@ dependencies = [
"digest",
]
[[package]]
name = "const-oid"
version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8"
[[package]]
name = "cpufeatures"
version = "0.2.11"
@ -192,12 +170,6 @@ dependencies = [
"libc",
]
[[package]]
name = "critical-section"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7059fff8937831a9ae6f0fe4d658ffabf58f2ca96aa9dec1c889f936f705f216"
[[package]]
name = "crypto-common"
version = "0.1.6"
@ -232,7 +204,6 @@ dependencies = [
"platforms",
"rustc_version",
"subtle",
"zeroize",
]
[[package]]
@ -255,15 +226,6 @@ dependencies = [
"generic-array",
]
[[package]]
name = "der"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fffa369a668c8af7dbf8b5e56c9f744fbd399949ed171606040001947de40b1c"
dependencies = [
"const-oid",
]
[[package]]
name = "digest"
version = "0.10.7"
@ -281,7 +243,6 @@ version = "2.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53"
dependencies = [
"pkcs8",
"signature",
]
@ -293,9 +254,7 @@ checksum = "1f628eaec48bfd21b865dc2950cfa014450c01d2fa2b69a86c2fd5844ec523c0"
dependencies = [
"curve25519-dalek",
"ed25519",
"rand_core",
"sha2",
"signature",
"subtle",
]
@ -337,28 +296,6 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]]
name = "hash32"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67"
dependencies = [
"byteorder",
]
[[package]]
name = "heapless"
version = "0.7.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdc6457c0eb62c71aac4bc17216026d8410337c4126773b9c5daba343f17964f"
dependencies = [
"atomic-polyfill",
"hash32",
"rustc_version",
"spin",
"stable_deref_trait",
]
[[package]]
name = "home"
version = "0.5.9"
@ -423,16 +360,6 @@ version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456"
[[package]]
name = "lock_api"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.20"
@ -482,16 +409,6 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
[[package]]
name = "pkcs8"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7"
dependencies = [
"der",
"spki",
]
[[package]]
name = "pkg-config"
version = "0.3.27"
@ -604,12 +521,6 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "semver"
version = "1.0.20"
@ -649,33 +560,6 @@ name = "signature"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de"
dependencies = [
"digest",
]
[[package]]
name = "spin"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
dependencies = [
"lock_api",
]
[[package]]
name = "spki"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d"
dependencies = [
"der",
]
[[package]]
name = "stable_deref_trait"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
[[package]]
name = "static_assertions"

View File

@ -11,16 +11,18 @@ assert_no_alloc = "1.1.2"
# assert_no_alloc = {version="1.1.2", features=["warn_debug"]}
ibig = "0.3.6"
# optional features
curve25519-dalek = {version = "4.1.1", optional = true }
ed25519-dalek = { version = "2.1.0", features = ["digest", "pkcs8", "rand_core"], default-features = false, optional = true }
# ed25519
curve25519-dalek = {version = "4.1.1", default-features = false, optional = true }
ed25519-dalek = { version = "2.1.0", default-features = false, optional = true }
x25519-dalek = { version = "2.0.0", features = ["static_secrets"], default-features = false, optional = true}
aes = { version = "0.8.3", optional = true }
aes-siv = { version = "0.7.0", features = ["heapless"], default-features = false, optional = true }
# aes_siv
aes = { version = "0.8.3", default-features = false, optional = true }
aes-siv = { version = "0.7.0", default-features = false, optional = true }
sha1 = { version = "0.10.6", optional = true }
sha2 = { version = "0.10.8", optional = true }
# sha
sha1 = { version = "0.10.6", default-features = false, optional = true }
sha2 = { version = "0.10.8", default-features = false, optional = true }
urcrypt-sys = { version = "0.1.1", optional = true }

View File

@ -3,3 +3,6 @@ pub mod aes_siv;
#[cfg(feature = "ed25519")]
pub mod ed25519;
#[cfg(feature = "sha")]
pub mod sha;

View File

@ -1,32 +1,116 @@
use sha::sha1::Sha1;
use sha::sha2::{Sha256, Sha512};
use sha::utils::{Digest, DigestExt};
use sha1::{Digest, Sha1};
use sha2::{Sha256, Sha512};
pub fn ac_sha1(message: &mut [u8], out: &mut [u8]) {
/// Hashes a message using SHA-1.
pub fn ac_sha1(message: &mut [u8], out: &mut [u8; 20]) {
message.reverse();
let result = Sha1::default().digest(message);
println!("result: {:?}", result);
let mut hasher = Sha1::new();
hasher.update(message);
let mut result = hasher.finalize();
result.reverse();
out.copy_from_slice(&result);
}
/// Hashes a message using SHA-256.
pub fn ac_shay(message: &mut [u8], out: &mut [u8; 32]) {
let mut hasher = Sha256::new();
hasher.update(message);
let result = hasher.finalize();
out.copy_from_slice(&result);
}
/// Hashes a message using SHA-512.
pub fn ac_shal(message: &mut [u8], out: &mut [u8; 64]) {
let mut hasher = Sha512::new();
hasher.update(message);
let result = hasher.finalize();
out.copy_from_slice(&result);
}
/// Hashes a message and salt using SHA-256.
pub fn ac_shas(message: &mut [u8], salt: &mut [u8], out: &mut [u8; 32]) {
let mut mid: [u8; 32] = [0; 32];
ac_shay(message, &mut mid);
if salt.len() > 32 {
for i in 0..32 {
salt[i] ^= mid[i];
}
ac_shay(salt, out);
}
else {
for i in 0..salt.len() {
mid[i] ^= salt[i];
}
ac_shay(&mut mid, out);
}
}
#[cfg(test)]
#[cfg(feature = "test_vs_urcrypt")]
mod urcrypt_tests {
use super::{ac_sha1};
use ibig::ubig;
use urcrypt_sys::{urcrypt_sha1};
use super::{ac_sha1, ac_shal, ac_shas, ac_shay};
use urcrypt_sys::{urcrypt_sha1, urcrypt_shal, urcrypt_shas, urcrypt_shay};
#[test]
fn test_sha1() {
let mut message: [u8; 32] = [42; 32];
let mut uc_out: [u8; 32] = [0; 32];
unsafe { urcrypt_sha1(message.as_ptr(), uc_out.as_mut_ptr()) };
let mut uc_out: [u8; 20] = [0; 20];
unsafe { urcrypt_sha1(message.as_mut_ptr(), message.len(), uc_out.as_mut_ptr()) };
let mut ac_out: [u8; 32] = [0; 32];
let mut ac_out: [u8; 20] = [0; 20];
ac_sha1(&mut message, &mut ac_out);
assert_eq!(ac_out, uc_out);
}
}
#[test]
fn test_shay() {
let mut message: [u8; 32] = [42; 32];
let mut uc_out: [u8; 32] = [0; 32];
unsafe { urcrypt_shay(message.as_mut_ptr(), message.len(), uc_out.as_mut_ptr()) };
let mut ac_out: [u8; 32] = [0; 32];
ac_shay(&mut message, &mut ac_out);
assert_eq!(ac_out, uc_out);
}
#[test]
fn test_shal() {
let mut message: [u8; 32] = [42; 32];
let mut uc_out: [u8; 64] = [0; 64];
unsafe { urcrypt_shal(message.as_mut_ptr(), message.len(), uc_out.as_mut_ptr()) };
let mut ac_out: [u8; 64] = [0; 64];
ac_shal(&mut message, &mut ac_out);
assert_eq!(ac_out, uc_out);
}
#[test]
fn test_shas() {
let mut message: [u8; 32] = [42; 32];
let mut uc_salt: [u8; 32] = [43; 32];
let mut uc_out: [u8; 32] = [0; 32];
unsafe {
urcrypt_shas(
uc_salt.as_mut_ptr(),
uc_salt.len(),
message.as_ptr(),
message.len(),
uc_out.as_mut_ptr(),
)
};
let mut ac_salt: [u8; 32] = [43; 32];
let mut ac_out: [u8; 32] = [0; 32];
ac_shas(&mut message, &mut ac_salt, &mut ac_out);
assert_eq!(ac_out, uc_out);
}
}