From 78ead7344eeb7aacdc2ade80ffd37ad725cb1ce6 Mon Sep 17 00:00:00 2001 From: Casper Date: Tue, 16 Feb 2021 02:14:06 +0100 Subject: [PATCH] Rename the consts to harden against accidental misuse --- src/cfo.rs | 22 ++++++++------- src/lib.rs | 73 ++++++++++++++++++++++++-------------------------- src/murmur3.rs | 4 +-- src/wide.rs | 8 +++--- 4 files changed, 53 insertions(+), 54 deletions(-) diff --git a/src/cfo.rs b/src/cfo.rs index 08a89bb..761b8c6 100644 --- a/src/cfo.rs +++ b/src/cfo.rs @@ -46,17 +46,19 @@ pub const fn generate(mut key: u32, mut xor: u32, stmts: &[&'s macro_rules! obfstmt { ($($stmt:stmt;)*) => {{ // Initial KEY and XOR values - const KEY: u32 = $crate::random!(u32); - const XOR: u32 = $crate::murmur3(b"XOR", KEY); + const _OBFSTMT_KEY: u32 = $crate::random!(u32); + const _OBFSTMT_XOR: u32 = $crate::murmur3(b"XOR", _OBFSTMT_KEY); // Count the number of statements - const COUNT: usize = <[&'static str]>::len(&[$(stringify!($stmt)),*]); + const _OBFSTMT_LEN: usize = <[&'static str]>::len(&[$(stringify!($stmt)),*]); // Generate key and xor values of every statement and the final exit code - const STMTS: [(&'static str, u32, u32); COUNT] = $crate::cfo::generate::<{COUNT}>(KEY, XOR, &[$(stringify!($stmt)),*]); - const EXIT: u32 = if COUNT == 0 { KEY ^ XOR } else { STMTS[COUNT - 1].1 ^ STMTS[COUNT - 1].2 }; + const _OBFSTMT_STMTS: [(&'static str, u32, u32); _OBFSTMT_LEN] = + $crate::cfo::generate::<{_OBFSTMT_LEN}>(_OBFSTMT_KEY, _OBFSTMT_XOR, &[$(stringify!($stmt)),*]); + const _OBFSTMT_EXIT: u32 = if _OBFSTMT_LEN == 0 { _OBFSTMT_KEY ^ _OBFSTMT_XOR } + else { _OBFSTMT_STMTS[_OBFSTMT_LEN - 1].1 ^ _OBFSTMT_STMTS[_OBFSTMT_LEN - 1].2 }; // Initialize the key and xor values - let mut key = KEY; + let mut key = _OBFSTMT_KEY; #[allow(unused_mut)] - let mut xor = XOR; + let mut xor = _OBFSTMT_XOR; loop { $crate::obfstmt_match!(key, xor, 0usize, [$($stmt;)*], []); key ^= xor; @@ -74,12 +76,12 @@ macro_rules! obfstmt_match { // Have to use match guard here because an expression isn't allowed in pattern position // The result is still optimized to a binary search for the right key per block $( - key if key == { STMTS[$i].1 } => { + key if key == { _OBFSTMT_STMTS[$i].1 } => { $stmt - $xor = STMTS[$i].2; + $xor = _OBFSTMT_STMTS[$i].2; }, )* - EXIT => break, + _OBFSTMT_EXIT => break, _ => (), } }; diff --git a/src/lib.rs b/src/lib.rs index d4c1de6..e586173 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,7 +42,7 @@ pub use self::murmur3::murmur3; /// ``` #[macro_export] macro_rules! random { - ($ty:ident) => {{ const ENTROPY: u64 = $crate::entropy(file!(), line!(), column!()); $crate::random!($ty, ENTROPY) }}; + ($ty:ident) => {{ const _RANDOM_ENTROPY: u64 = $crate::entropy(file!(), line!(), column!()); $crate::random!($ty, _RANDOM_ENTROPY) }}; (u8, $seed:expr) => { $seed as u8 }; (u16, $seed:expr) => { $seed as u16 }; @@ -98,7 +98,7 @@ pub const fn hash(s: &str) -> u32 { /// ``` #[macro_export] macro_rules! hash { - ($s:expr) => {{ const HASH: u32 = $crate::hash($s); HASH }}; + ($s:expr) => {{ const _HASH_PTNS: u32 = $crate::hash($s); _HASH_PTNS }}; } /// Produces pseudorandom entropy given the file, line and column information. @@ -127,10 +127,7 @@ pub const SEED: u64 = splitmix(hash(env!("OBFSTR_SEED")) as u64); /// ``` #[macro_export] macro_rules! position { - ($haystack:expr, $needle:expr) => {{ - const POSITION: ::core::ops::Range = $crate::position($haystack, $needle); - POSITION - }}; + ($haystack:expr, $needle:expr) => {{ const _POSITION_RANGE: ::core::ops::Range = $crate::position($haystack, $needle); _POSITION_RANGE }}; } /// Finds the position of the needle in the haystack at compiletime. @@ -445,56 +442,56 @@ pub fn unsafe_as_str(bytes: &[u8]) -> &str { #[macro_export] macro_rules! obfstr { ($buf:ident <- $s:expr) => {{ - const STRING: &str = $s; - const LEN: usize = STRING.len(); - const KEYSTREAM: [u8; LEN] = $crate::bytes::keystream::($crate::random!(u32)); - static mut OBFSTRING: [u8; LEN] = $crate::bytes::obfuscate::(STRING.as_bytes(), &KEYSTREAM); - let buf = &mut $buf[..LEN]; - buf.copy_from_slice(&$crate::bytes::deobfuscate::($crate::xref(unsafe { &OBFSTRING }, $crate::random!(usize) & 0xffff), &KEYSTREAM)); + const _OBFSTR_STRING: &str = $s; + const _OBFSTR_LEN: usize = _OBFSTR_STRING.len(); + const _OBFSTR_KEYSTREAM: [u8; _OBFSTR_LEN] = $crate::bytes::keystream::<_OBFSTR_LEN>($crate::random!(u32)); + static mut _OBFSTR_DATA: [u8; _OBFSTR_LEN] = $crate::bytes::obfuscate::<_OBFSTR_LEN>(_OBFSTR_STRING.as_bytes(), &_OBFSTR_KEYSTREAM); + let buf = &mut $buf[.._OBFSTR_LEN]; + buf.copy_from_slice(&$crate::bytes::deobfuscate::<_OBFSTR_LEN>($crate::xref(unsafe { &_OBFSTR_DATA }, $crate::random!(usize) & 0xffff), &_OBFSTR_KEYSTREAM)); $crate::unsafe_as_str(buf) }}; ($buf:ident <- L$s:expr) => {{ - const STRING: &[u16] = $crate::wide!($s); - const LEN: usize = STRING.len(); - const KEYSTREAM: [u16; LEN] = $crate::words::keystream::($crate::random!(u32)); - static mut OBFSTRING: [u16; LEN] = $crate::words::obfuscate::(STRING, &KEYSTREAM); - let buf = &mut $buf[..LEN]; - buf.copy_from_slice(&$crate::words::deobfuscate::($crate::xref(unsafe { &OBFSTRING }, $crate::random!(usize) & 0xffff), &KEYSTREAM)); + const _OBFSTR_STRING: &[u16] = $crate::wide!($s); + const _OBFSTR_LEN: usize = _OBFSTR_STRING.len(); + const _OBFSTR_KEYSTREAM: [u16; _OBFSTR_LEN] = $crate::words::keystream::<_OBFSTR_LEN>($crate::random!(u32)); + static mut _OBFSTR_DATA: [u16; _OBFSTR_LEN] = $crate::words::obfuscate::<_OBFSTR_LEN>(_OBFSTR_STRING, &_OBFSTR_KEYSTREAM); + let buf = &mut $buf[.._OBFSTR_LEN]; + buf.copy_from_slice(&$crate::words::deobfuscate::<_OBFSTR_LEN>($crate::xref(unsafe { &_OBFSTR_DATA }, $crate::random!(usize) & 0xffff), &_OBFSTR_KEYSTREAM)); buf }}; ($s:expr) => {{ - const STRING: &str = $s; - const LEN: usize = STRING.len(); - const KEYSTREAM: [u8; LEN] = $crate::bytes::keystream::($crate::random!(u32)); - static mut OBFSTRING: [u8; LEN] = $crate::bytes::obfuscate::(STRING.as_bytes(), &KEYSTREAM); - $crate::unsafe_as_str(&$crate::bytes::deobfuscate::($crate::xref(unsafe { &OBFSTRING }, $crate::random!(usize) & 0xffff), &KEYSTREAM)) + const _OBFSTR_STRING: &str = $s; + const _OBFSTR_LEN: usize = _OBFSTR_STRING.len(); + const _OBFSTR_KEYSTREAM: [u8; _OBFSTR_LEN] = $crate::bytes::keystream::<_OBFSTR_LEN>($crate::random!(u32)); + static mut _OBFSTR_DATA: [u8; _OBFSTR_LEN] = $crate::bytes::obfuscate::<_OBFSTR_LEN>(_OBFSTR_STRING.as_bytes(), &_OBFSTR_KEYSTREAM); + $crate::unsafe_as_str(&$crate::bytes::deobfuscate::<_OBFSTR_LEN>($crate::xref(unsafe { &_OBFSTR_DATA }, $crate::random!(usize) & 0xffff), &_OBFSTR_KEYSTREAM)) }}; (L$s:expr) => {{ - const STRING: &[u16] = $crate::wide!($s); - const LEN: usize = STRING.len(); - const KEYSTREAM: [u16; LEN] = $crate::words::keystream::($crate::random!(u32)); - static mut OBFSTRING: [u16; LEN] = $crate::words::obfuscate::(STRING, &KEYSTREAM); - &$crate::words::deobfuscate::($crate::xref(unsafe { &OBFSTRING }, $crate::random!(usize) & 0xffff), &KEYSTREAM) + const _OBFSTR_STRING: &[u16] = $crate::wide!($s); + const _OBFSTR_LEN: usize = _OBFSTR_STRING.len(); + const _OBFSTR_KEYSTREAM: [u16; _OBFSTR_LEN] = $crate::words::keystream::<_OBFSTR_LEN>($crate::random!(u32)); + static mut _OBFSTR_DATA: [u16; _OBFSTR_LEN] = $crate::words::obfuscate::<_OBFSTR_LEN>(_OBFSTR_STRING, &_OBFSTR_KEYSTREAM); + &$crate::words::deobfuscate::<_OBFSTR_LEN>($crate::xref(unsafe { &_OBFSTR_DATA }, $crate::random!(usize) & 0xffff), &_OBFSTR_KEYSTREAM) }}; ($(let $name:ident = $s:expr;)*) => {$( let $name = { - const STRING: &str = $s; - const LEN: usize = STRING.len(); - const KEYSTREAM: [u8; LEN] = $crate::bytes::keystream::($crate::random!(u32)); - static mut OBFSTRING: [u8; LEN] = $crate::bytes::obfuscate::(STRING.as_bytes(), &KEYSTREAM); - $crate::bytes::deobfuscate::($crate::xref(unsafe { &OBFSTRING }, $crate::random!(usize) & 0xffff), &KEYSTREAM) + const _OBFSTR_STRING: &str = $s; + const _OBFSTR_LEN: usize = _OBFSTR_STRING.len(); + const _OBFSTR_KEYSTREAM: [u8; _OBFSTR_LEN] = $crate::bytes::keystream::<_OBFSTR_LEN>($crate::random!(u32)); + static mut _OBFSTR_DATA: [u8; _OBFSTR_LEN] = $crate::bytes::obfuscate::<_OBFSTR_LEN>(_OBFSTR_STRING.as_bytes(), &_OBFSTR_KEYSTREAM); + $crate::bytes::deobfuscate::<_OBFSTR_LEN>($crate::xref(unsafe { &_OBFSTR_DATA }, $crate::random!(usize) & 0xffff), &_OBFSTR_KEYSTREAM) }; let $name = $crate::unsafe_as_str(&$name); )*}; ($(let $name:ident = L$s:expr;)*) => {$( let $name = { - const STRING: &[u16] = $crate::wide!($s); - const LEN: usize = STRING.len(); - const KEYSTREAM: [u16; LEN] = $crate::words::keystream::($crate::random!(u32)); - static mut OBFSTRING: [u16; LEN] = $crate::words::obfuscate::(STRING, &KEYSTREAM); - $crate::words::deobfuscate::($crate::xref(unsafe { &OBFSTRING }, $crate::random!(usize) & 0xffff), &KEYSTREAM) + const _OBFSTR_STRING: &[u16] = $crate::wide!($s); + const _OBFSTR_LEN: usize = _OBFSTR_STRING.len(); + const _OBFSTR_KEYSTREAM: [u16; _OBFSTR_LEN] = $crate::words::keystream::<_OBFSTR_LEN>($crate::random!(u32)); + static mut _OBFSTR_DATA: [u16; _OBFSTR_LEN] = $crate::words::obfuscate::<_OBFSTR_LEN>(_OBFSTR_STRING, &_OBFSTR_KEYSTREAM); + $crate::words::deobfuscate::<_OBFSTR_LEN>($crate::xref(unsafe { &_OBFSTR_DATA }, $crate::random!(usize) & 0xffff), &_OBFSTR_KEYSTREAM) }; let $name = &$name; )*}; diff --git a/src/murmur3.rs b/src/murmur3.rs index a75e69c..0b62143 100644 --- a/src/murmur3.rs +++ b/src/murmur3.rs @@ -6,8 +6,8 @@ Expose MurmurHash3, a keyed hash function. Not ready for public API. /// MurmurHash3 (32-bit variant) keyed hash function. #[macro_export] macro_rules! murmur3 { - ($string:expr, $seed:expr) => {{ const HASH: u32 = $crate::murmur3($string, $seed); HASH }}; - ($string:expr) => {{ const HASH: u32 = $crate::murmur3($string, 0); HASH }}; + ($s:expr, $seed:expr) => {{ const _MURMUR3_HASH: u32 = $crate::murmur3($s, $seed); _MURMUR3_HASH }}; + ($s:expr) => {{ const _MURMUR3_HASH: u32 = $crate::murmur3($s, 0); _MURMUR3_HASH }}; } #[doc(hidden)] diff --git a/src/wide.rs b/src/wide.rs index add4ef4..f8d70d6 100644 --- a/src/wide.rs +++ b/src/wide.rs @@ -12,10 +12,10 @@ #[macro_export] macro_rules! wide { ($s:expr) => {{ - const _OBFSTR_RPWZ_STRING: &str = $s; - const _OBFSTR_NKVU_LEN: usize = $crate::wide::len(_OBFSTR_RPWZ_STRING); - const _OBFSTR_CVUA_WIDE: [u16; _OBFSTR_NKVU_LEN] = $crate::wide::encode::<_OBFSTR_NKVU_LEN>(_OBFSTR_RPWZ_STRING); - &_OBFSTR_CVUA_WIDE + const _WIDE_STRING: &str = $s; + const _WIDE_LEN: usize = $crate::wide::len(_WIDE_STRING); + const _WIDE_WORDS: [u16; _WIDE_LEN] = $crate::wide::encode::<_WIDE_LEN>(_WIDE_STRING); + &_WIDE_WORDS }}; }