Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve docs and stuff #52

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ The string constant itself is embedded in obfuscated form and deobfuscated local
This reference to a temporary value must be used in the same statement it was generated.
See the documentation for more advanced use cases.

If you're looking for obfuscating format strings (`format!`, `println!`, etc.) I have another crate [`fmtools`](https://crates.io/crates/fmtools) with the optional dependency `obfstr` enabled to automatically apply string obfuscation to your formatting strings.
Looking for obfuscating formatting strings? See `fmtools` ([github](https://github.com/CasualX/fmtools), [crates.io](https://crates.io/crates/fmtools), [docs.rs](https://docs.rs/fmtools/0.1.2/fmtools/)) with the optional dependency `obfstr` enabled to automatically apply string obfuscation to formatting strings.

Examples
--------

The `obfstr!` macro returns the deobfuscated string as a temporary value:

```rust
assert_eq!(obfstr::obfstr!("Hello 🌍"), "Hello 🌍");
use obfstr::obfstr as s;
assert_eq!(s!("Hello 🌍"), "Hello 🌍");
```

The `wide!` macro provides compiletime utf16 string constants:
Expand Down
22 changes: 11 additions & 11 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ use core::ptr::{read_volatile, write};
/// The `obfstr!` macro returns the deobfuscated string as a temporary `&str` value and must be consumed in the same statement it was used:
///
/// ```
/// use obfstr::obfstr;
/// use obfstr::obfstr as s;
///
/// const HELLO_WORLD: &str = "Hello 🌍";
/// assert_eq!(obfstr!(HELLO_WORLD), HELLO_WORLD);
/// assert_eq!(s!(HELLO_WORLD), HELLO_WORLD);
/// ```
///
/// Different syntax forms are supported to reuse the obfuscated strings in outer scopes:
///
/// ```
/// use obfstr::obfstr;
/// use obfstr::obfstr as s;
///
/// // Obfuscate a bunch of strings
/// obfstr! {
/// s! {
/// let s = "Hello world";
/// let another = "another";
/// }
Expand All @@ -36,16 +36,16 @@ use core::ptr::{read_volatile, write};
/// // Assign to an uninit variable in outer scope
/// let (true_string, false_string);
/// let string = if true {
/// obfstr!(true_string = "true")
/// s!(true_string = "true")
/// }
/// else {
/// obfstr!(false_string = "false")
/// s!(false_string = "false")
/// };
/// assert_eq!(string, "true");
///
/// // Return an obfuscated string from a function
/// fn helper(buf: &mut [u8]) -> &str {
/// obfstr!(buf <- "hello")
/// s!(buf <- "hello")
/// }
/// let mut buf = [0u8; 16];
/// assert_eq!(helper(&mut buf), "hello");
Expand Down Expand Up @@ -96,10 +96,10 @@ macro_rules! __obfbytes {
const _OBFBYTES_KEYSTREAM: [u8; _OBFBYTES_LEN] = $crate::bytes::keystream::<_OBFBYTES_LEN>($crate::__entropy!("key", stringify!($s)) as u32);
static _OBFBYTES_SDATA: [u8; _OBFBYTES_LEN] = $crate::bytes::obfuscate::<_OBFBYTES_LEN>(_OBFBYTES_STRING, &_OBFBYTES_KEYSTREAM);
$crate::bytes::deobfuscate::<_OBFBYTES_LEN>(
$crate::__xref!(
$crate::__entropy!("offset", stringify!($s)) as usize,
$crate::__entropy!("xref", stringify!($s)),
&_OBFBYTES_SDATA),
$crate::xref::xref::<_,
{$crate::__entropy!("offset", stringify!($s)) as usize},
{$crate::__entropy!("xref", stringify!($s))}>
(&_OBFBYTES_SDATA),
&_OBFBYTES_KEYSTREAM)
}};
}
Expand Down
8 changes: 4 additions & 4 deletions src/words.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ macro_rules! __obfwide {
const _OBFWIDE_KEYSTREAM: [u16; _OBFWIDE_LEN] = $crate::words::keystream::<_OBFWIDE_LEN>($crate::__entropy!("key", stringify!($s)) as u32);
static _OBFWIDE_SDATA: [u16; _OBFWIDE_LEN] = $crate::words::obfuscate::<_OBFWIDE_LEN>(_OBFWIDE_STRING, &_OBFWIDE_KEYSTREAM);
$crate::words::deobfuscate::<_OBFWIDE_LEN>(
$crate::__xref!(
$crate::__entropy!("offset", stringify!($s)) as usize,
$crate::__entropy!("xref", stringify!($s)),
&_OBFWIDE_SDATA),
$crate::xref::xref::<_,
{$crate::__entropy!("offset", stringify!($s)) as usize},
{$crate::__entropy!("xref", stringify!($s))}>
(&_OBFWIDE_SDATA),
&_OBFWIDE_KEYSTREAM)
}};
}
Expand Down
24 changes: 6 additions & 18 deletions src/xref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,9 @@ use core::hint;
#[macro_export]
macro_rules! xref {
($e:expr) => {
$crate::__xref!($crate::__entropy!(stringify!($e), 1) as usize, $crate::__entropy!(stringify!($e), 2), $e)
};
}

#[doc(hidden)]
#[macro_export]
macro_rules! __xref {
($offset:expr, $seed:expr, $e:expr) => {
$crate::xref::xref::<_, {$offset}, {$seed}>($e)
$crate::xref::xref::<_,
{$crate::__entropy!(stringify!($e), 1) as usize},
{$crate::__entropy!(stringify!($e), 2)}>($e)
};
}

Expand Down Expand Up @@ -98,15 +92,9 @@ pub fn xref<T: ?Sized, const OFFSET: usize, const SEED: u64>(p: &'static T) -> &
#[macro_export]
macro_rules! xref_mut {
($e:expr) => {
$crate::__xref_mut!($crate::__entropy!(stringify!($e), 1) as usize, $crate::__entropy!(stringify!($e), 2), $e)
};
}

#[doc(hidden)]
#[macro_export]
macro_rules! __xref_mut {
($offset:expr, $seed:expr, $e:expr) => {
$crate::xref::xref_mut::<_, {$offset}, {$seed}>($e)
$crate::xref::xref_mut::<_,
{$crate::__entropy!(stringify!($e), 1) as usize},
{$crate::__entropy!(stringify!($e), 2)}>($e)
};
}

Expand Down