Skip to content

Commit

Permalink
Rename the consts to harden against accidental misuse
Browse files Browse the repository at this point in the history
  • Loading branch information
CasualX committed Feb 16, 2021
1 parent 809ce69 commit 78ead73
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 54 deletions.
22 changes: 12 additions & 10 deletions src/cfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ pub const fn generate<const LEN: usize>(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;
Expand All @@ -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,
_ => (),
}
};
Expand Down
73 changes: 35 additions & 38 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<usize> = $crate::position($haystack, $needle);
POSITION
}};
($haystack:expr, $needle:expr) => {{ const _POSITION_RANGE: ::core::ops::Range<usize> = $crate::position($haystack, $needle); _POSITION_RANGE }};
}

/// Finds the position of the needle in the haystack at compiletime.
Expand Down Expand Up @@ -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::<LEN>($crate::random!(u32));
static mut OBFSTRING: [u8; LEN] = $crate::bytes::obfuscate::<LEN>(STRING.as_bytes(), &KEYSTREAM);
let buf = &mut $buf[..LEN];
buf.copy_from_slice(&$crate::bytes::deobfuscate::<LEN>($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::<LEN>($crate::random!(u32));
static mut OBFSTRING: [u16; LEN] = $crate::words::obfuscate::<LEN>(STRING, &KEYSTREAM);
let buf = &mut $buf[..LEN];
buf.copy_from_slice(&$crate::words::deobfuscate::<LEN>($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::<LEN>($crate::random!(u32));
static mut OBFSTRING: [u8; LEN] = $crate::bytes::obfuscate::<LEN>(STRING.as_bytes(), &KEYSTREAM);
$crate::unsafe_as_str(&$crate::bytes::deobfuscate::<LEN>($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::<LEN>($crate::random!(u32));
static mut OBFSTRING: [u16; LEN] = $crate::words::obfuscate::<LEN>(STRING, &KEYSTREAM);
&$crate::words::deobfuscate::<LEN>($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::<LEN>($crate::random!(u32));
static mut OBFSTRING: [u8; LEN] = $crate::bytes::obfuscate::<LEN>(STRING.as_bytes(), &KEYSTREAM);
$crate::bytes::deobfuscate::<LEN>($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::<LEN>($crate::random!(u32));
static mut OBFSTRING: [u16; LEN] = $crate::words::obfuscate::<LEN>(STRING, &KEYSTREAM);
$crate::words::deobfuscate::<LEN>($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;
)*};
Expand Down
4 changes: 2 additions & 2 deletions src/murmur3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
8 changes: 4 additions & 4 deletions src/wide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}};
}

Expand Down

0 comments on commit 78ead73

Please sign in to comment.