obfany 0.1.1

Compiletime string and number constant obfuscation for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*!

Byte string obfuscation
=======================
*/

use core::ptr::{read_volatile, write};

/// Compiletime string constant obfuscation.
///
/// The purpose of the obfuscation is to make it difficult to discover the original strings with automated analysis.
/// String obfuscation is not intended to hinder a dedicated reverse engineer from discovering the original string.
/// This should not be used to hide secrets in client binaries and the author disclaims any responsibility for any damages resulting from ignoring this warning.
///
/// The `obfstr!` macro returns the deobfuscated string as a temporary `&str` value and must be consumed in the same statement it was used:
///
/// ```
/// use obfany::obfstr as s;
///
/// const HELLO_WORLD: &str = "Hello 🌍";
/// assert_eq!(s!(HELLO_WORLD), HELLO_WORLD);
/// ```
///
/// Different syntax forms are supported to reuse the obfuscated strings in outer scopes:
///
/// ```
/// use obfany::obfstr as s;
///
/// // Obfuscate a bunch of strings
/// s! {
/// 	let s = "Hello world";
/// 	let another = "another";
/// }
/// assert_eq!(s, "Hello world");
/// assert_eq!(another, "another");
///
/// // Assign to an uninit variable in outer scope
/// let (true_string, false_string);
/// let string = if true {
/// 	s!(true_string = "true")
/// }
/// else {
/// 	s!(false_string = "false")
/// };
/// assert_eq!(string, "true");
///
/// // Return an obfuscated string from a function
/// fn helper(buf: &mut [u8]) -> &str {
/// 	s!(buf <- "hello")
/// }
/// let mut buf = [0u8; 16];
/// assert_eq!(helper(&mut buf), "hello");
/// ```
#[macro_export]
macro_rules! obfstr {
	($(let $name:ident = $s:expr;)*) => {$(
		$crate::obfbytes! { let $name = ::core::primitive::str::as_bytes($s); }
		let $name = $crate::unsafe_as_str($name);
	)*};
	($name:ident = $s:expr) => {
		$crate::unsafe_as_str($crate::obfbytes!($name = ::core::primitive::str::as_bytes($s)))
	};
	($buf:ident <- $s:expr) => {
		$crate::unsafe_as_str($crate::obfbytes!($buf <- ::core::primitive::str::as_bytes($s)))
	};
	($s:expr) => {
		$crate::unsafe_as_str($crate::obfbytes!(::core::primitive::str::as_bytes($s)))
	};
}

/// Compiletime cstr constant obfuscation.
///
/// See [`obfstr!`] for more information.
///
/// ```
/// use std::ffi::CStr;
/// use obfany::obfcstr as cstr;
///
/// const HELLO_WORLD: &'static CStr = c"Hello CStr";
/// assert_eq!(cstr!(HELLO_WORLD).to_str().unwrap(), "Hello CStr");
/// ```
#[macro_export]
macro_rules! obfcstr {
	($(let $name:ident = $s:expr;)*) => {$(
		$crate::obfbytes! { let $name = ::core::ffi::CStr::to_bytes_with_nul($s); }
		let $name = $crate::unsafe_as_cstr($name);
	)*};
	($name:ident = $s:expr) => {
		$crate::unsafe_as_cstr($crate::obfbytes!($name = ::core::ffi::CStr::to_bytes_with_nul($s)))
	};
	($buf:ident <- $s:expr) => {
		$crate::unsafe_as_cstr($crate::obfbytes!($buf <- ::core::ffi::CStr::to_bytes_with_nul($s)))
	};
	($s:expr) => {
		$crate::unsafe_as_cstr($crate::obfbytes!(::core::ffi::CStr::to_bytes_with_nul($s)))
	};
}

/// Compiletime string constant obfuscation.
///
/// Returns an owned `String` instead of a temporary `&str`.
///
/// See [`obfstr!`] for more information.
#[macro_export]
macro_rules! obfstring {
    ($s:expr) => {
        String::from($crate::obfstr!($s))
    };
}

/// Compiletime number constant obfuscation.
///
/// Stores an integer or float constant in XOR-encrypted form inside the binary.
/// The plaintext value is only reconstructed at runtime using a compile-time keystream,
/// making it harder for static analysis tools to find the original value.
///
/// Works with all primitive numeric types: `u8`, `u16`, `u32`, `u64`, `u128`,
/// `i8`, `i16`, `i32`, `i64`, `i128`, `usize`, `isize`, `f32`, `f64`.
///
/// Always use a typed literal suffix to ensure the size matches the expected return type.
///
/// ```
/// let secret: u32 = obfany::obfnum!(0x1234_u32);
/// assert_eq!(secret, 0x1234_u32);
///
/// let score: i64 = obfany::obfnum!(-9999_i64);
/// assert_eq!(score, -9999_i64);
///
/// let pi: f64 = obfany::obfnum!(3.14159265358979_f64);
/// assert!((pi - std::f64::consts::PI).abs() < 1e-10);
/// ```
#[macro_export]
macro_rules! obfnum {
    ($val:expr) => {{
        use ::core::primitive::*;
        const _OBFNUM_SIZE: usize = ::core::mem::size_of_val(&{ $val });
        const _OBFNUM_PLAIN: [u8; _OBFNUM_SIZE] = unsafe { ::core::mem::transmute({ $val }) };
        const _OBFNUM_KEYSTREAM: [u8; _OBFNUM_SIZE] = $crate::bytes::keystream::<_OBFNUM_SIZE>(
            $crate::random!(u32, "obfnum_key", stringify!($val)),
        );
        static _OBFNUM_SDATA: [u8; _OBFNUM_SIZE] =
            $crate::bytes::obfuscate::<_OBFNUM_SIZE>(&_OBFNUM_PLAIN, &_OBFNUM_KEYSTREAM);
        let _obfnum_deobf: [u8; _OBFNUM_SIZE] = $crate::bytes::deobfuscate::<_OBFNUM_SIZE>(
            $crate::xref::xref::<
                _,
                { $crate::random!(u32, "obfnum_offset", stringify!($val)) },
                { $crate::random!(u64, "obfnum_xref", stringify!($val)) },
            >(&_OBFNUM_SDATA),
            &_OBFNUM_KEYSTREAM,
        );
        unsafe { ::core::mem::transmute_copy(&_obfnum_deobf) }
    }};
}

/// Compiletime byte string obfuscation.
#[macro_export]
macro_rules! obfbytes {
	($(let $name:ident = $s:expr;)*) => {
		$(let ref $name = $crate::__obfbytes!($s);)*
	};
	($name:ident = $s:expr) => {{
		$name = $crate::__obfbytes!($s);
		&$name
	}};
	($buf:ident <- $s:expr) => {{
		let buf = &mut $buf[..$s.len()];
		buf.copy_from_slice(&$crate::__obfbytes!($s));
		buf
	}};
	($s:expr) => {
		&$crate::__obfbytes!($s)
	};
}

#[doc(hidden)]
#[macro_export]
macro_rules! __obfbytes {
    ($s:expr) => {{
        use ::core::primitive::*;
        const _OBFBYTES_STRING: &[u8] = $s;
        const _OBFBYTES_LEN: usize = _OBFBYTES_STRING.len();
        const _OBFBYTES_KEYSTREAM: [u8; _OBFBYTES_LEN] =
            $crate::bytes::keystream::<_OBFBYTES_LEN>($crate::random!(u32, "key", stringify!($s)));
        static _OBFBYTES_SDATA: [u8; _OBFBYTES_LEN] =
            $crate::bytes::obfuscate::<_OBFBYTES_LEN>(_OBFBYTES_STRING, &_OBFBYTES_KEYSTREAM);
        $crate::bytes::deobfuscate::<_OBFBYTES_LEN>(
            $crate::xref::xref::<
                _,
                { $crate::random!(u32, "offset", stringify!($s)) },
                { $crate::random!(u64, "xref", stringify!($s)) },
            >(&_OBFBYTES_SDATA),
            &_OBFBYTES_KEYSTREAM,
        )
    }};
}

// Simple XorShift to generate the key stream.
// Security doesn't matter, we just want a number of random-looking bytes.
#[inline(always)]
const fn next_round(mut x: u32) -> u32 {
    x ^= x << 13;
    x ^= x >> 17;
    x ^= x << 5;
    return x;
}

/// Generate the key stream for array of given length.
#[inline(always)]
pub const fn keystream<const LEN: usize>(key: u32) -> [u8; LEN] {
    let mut keys = [0u8; LEN];
    let mut round_key = key;
    let mut i = 0;
    // Calculate the key stream in chunks of 4 bytes
    while i < LEN & !3 {
        round_key = next_round(round_key);
        let kb = round_key.to_ne_bytes();
        keys[i + 0] = kb[0];
        keys[i + 1] = kb[1];
        keys[i + 2] = kb[2];
        keys[i + 3] = kb[3];
        i += 4;
    }
    // Calculate the remaining bytes of the key stream
    round_key = next_round(round_key);
    let kb = round_key.to_ne_bytes();
    match LEN % 4 {
        1 => {
            keys[i + 0] = kb[0];
        }
        2 => {
            keys[i + 0] = kb[0];
            keys[i + 1] = kb[1];
        }
        3 => {
            keys[i + 0] = kb[0];
            keys[i + 1] = kb[1];
            keys[i + 2] = kb[2];
        }
        _ => (),
    }
    return keys;
}

/// Obfuscates the input string and given key stream.
#[inline(always)]
pub const fn obfuscate<const LEN: usize>(s: &[u8], k: &[u8; LEN]) -> [u8; LEN] {
    if s.len() != LEN {
        panic!("input string len not equal to key stream len");
    }
    let mut data = [0u8; LEN];
    let mut i = 0usize;
    while i < LEN {
        data[i] = s[i] ^ k[i];
        i += 1;
    }
    return data;
}

/// Deobfuscates the obfuscated input string and given key stream.
#[inline(always)]
pub fn deobfuscate<const LEN: usize>(s: &[u8; LEN], k: &[u8; LEN]) -> [u8; LEN] {
    let mut buf = [0u8; LEN];
    let mut i = 0;
    // Try to tickle the LLVM optimizer in _just_ the right way
    // Use `read_volatile` to avoid constant folding a specific read and optimize the rest
    // Volatile reads of any size larger than 8 bytes appears to cause a bunch of one byte reads
    // Hand optimize in chunks of 8 and 4 bytes to avoid this
    unsafe {
        let src = s.as_ptr();
        let dest = buf.as_mut_ptr();
        // Process in chunks of 8 bytes on 64-bit targets
        #[cfg(target_pointer_width = "64")]
        while i < LEN & !7 {
            let ct = read_volatile(src.offset(i as isize) as *const [u8; 8]);
            let tmp = u64::from_ne_bytes([ct[0], ct[1], ct[2], ct[3], ct[4], ct[5], ct[6], ct[7]])
                ^ u64::from_ne_bytes([
                    k[i + 0],
                    k[i + 1],
                    k[i + 2],
                    k[i + 3],
                    k[i + 4],
                    k[i + 5],
                    k[i + 6],
                    k[i + 7],
                ]);
            write(dest.offset(i as isize) as *mut [u8; 8], tmp.to_ne_bytes());
            i += 8;
        }
        // Process in chunks of 4 bytes
        while i < LEN & !3 {
            let ct = read_volatile(src.offset(i as isize) as *const [u8; 4]);
            let tmp = u32::from_ne_bytes([ct[0], ct[1], ct[2], ct[3]])
                ^ u32::from_ne_bytes([k[i + 0], k[i + 1], k[i + 2], k[i + 3]]);
            write(dest.offset(i as isize) as *mut [u8; 4], tmp.to_ne_bytes());
            i += 4;
        }
        // Process the remaining bytes
        match LEN % 4 {
            1 => {
                let ct = read_volatile(src.offset(i as isize));
                write(dest.offset(i as isize), ct ^ k[i]);
            }
            2 => {
                let ct = read_volatile(src.offset(i as isize) as *const [u8; 2]);
                write(
                    dest.offset(i as isize) as *mut [u8; 2],
                    [ct[0] ^ k[i + 0], ct[1] ^ k[i + 1]],
                );
            }
            3 => {
                let ct = read_volatile(src.offset(i as isize) as *const [u8; 3]);
                write(
                    dest.offset(i as isize) as *mut [u8; 2],
                    [ct[0] ^ k[i + 0], ct[1] ^ k[i + 1]],
                );
                write(dest.offset(i as isize + 2), ct[2] ^ k[i + 2]);
            }
            _ => (),
        }
    }
    return buf;
}

#[inline(always)]
pub fn equals<const LEN: usize>(s: &[u8; LEN], k: &[u8; LEN], other: &[u8]) -> bool {
    if other.len() != LEN {
        return false;
    }
    let mut i = 0;
    // Try to tickle the LLVM optimizer in _just_ the right way
    // Use `read_volatile` to avoid constant folding a specific read and optimize the rest
    // Volatile reads of any size larger than 8 bytes appears to cause a bunch of one byte reads
    // Hand optimize in chunks of 8 and 4 bytes to avoid this
    unsafe {
        let src = s.as_ptr();
        // Process in chunks of 8 bytes on 64-bit targets
        #[cfg(target_pointer_width = "64")]
        while i < LEN & !7 {
            let ct = read_volatile(src.offset(i as isize) as *const [u8; 8]);
            let tmp = u64::from_ne_bytes([ct[0], ct[1], ct[2], ct[3], ct[4], ct[5], ct[6], ct[7]])
                ^ u64::from_ne_bytes([
                    k[i + 0],
                    k[i + 1],
                    k[i + 2],
                    k[i + 3],
                    k[i + 4],
                    k[i + 5],
                    k[i + 6],
                    k[i + 7],
                ]);
            let other = u64::from_ne_bytes([
                other[i + 0],
                other[i + 1],
                other[i + 2],
                other[i + 3],
                other[i + 4],
                other[i + 5],
                other[i + 6],
                other[i + 7],
            ]);
            if tmp != other {
                return false;
            }
            i += 8;
        }
        // Process in chunks of 4 bytes
        while i < LEN & !3 {
            let ct = read_volatile(src.offset(i as isize) as *const [u8; 4]);
            let tmp = u32::from_ne_bytes([ct[0], ct[1], ct[2], ct[3]])
                ^ u32::from_ne_bytes([k[i + 0], k[i + 1], k[i + 2], k[i + 3]]);
            let other =
                u32::from_ne_bytes([other[i + 0], other[i + 1], other[i + 2], other[i + 3]]);
            if tmp != other {
                return false;
            }
            i += 4;
        }
        // Process the remaining bytes
        match LEN % 4 {
            1 => {
                let ct = read_volatile(src.offset(i as isize));
                ct ^ k[i] == other[i]
            }
            2 => {
                let ct = read_volatile(src.offset(i as isize) as *const [u8; 2]);
                u16::from_ne_bytes([ct[0], ct[1]]) ^ u16::from_ne_bytes([k[i + 0], k[i + 1]])
                    == u16::from_ne_bytes([other[i + 0], other[i + 1]])
            }
            3 => {
                let ct = read_volatile(src.offset(i as isize) as *const [u8; 3]);
                u32::from_ne_bytes([ct[0], ct[1], ct[2], 0])
                    ^ u32::from_ne_bytes([k[i + 0], k[i + 1], k[i + 2], 0])
                    == u32::from_ne_bytes([other[i + 0], other[i + 1], other[i + 2], 0])
            }
            _ => true,
        }
    }
}

// Test correct processing of less than multiple of 8 lengths
#[test]
fn test_remaining_bytes() {
    const STRING: &[u8] = b"01234567ABCDEFGHI";
    fn test<const LEN: usize>(key: u32) {
        let keys = keystream::<LEN>(key);
        let data = obfuscate::<LEN>(&STRING[..LEN], &keys);
        let buffer = deobfuscate::<LEN>(&data, &keys);
        // Ciphertext should not equal input string
        assert_ne!(&data[..], &STRING[..LEN]);
        // Deobfuscated result should equal input string
        assert_eq!(&buffer[..], &STRING[..LEN]);
        // Specialized equals check should succeed
        assert!(equals::<LEN>(&data, &keys, &STRING[..LEN]));
    }
    test::<8>(0x1111);
    test::<9>(0x2222);
    test::<10>(0x3333);
    test::<11>(0x4444);
    test::<12>(0x5555);
    test::<13>(0x6666);
    test::<14>(0x7777);
    test::<15>(0x8888);
    test::<16>(0x9999);
}

#[test]
fn test_equals() {
    const STRING: &str = "Hello 🌍";
    const LEN: usize = STRING.len();
    const KEYSTREAM: [u8; LEN] = keystream::<LEN>(0x10203040);
    const OBFSTRING: [u8; LEN] = obfuscate::<LEN>(STRING.as_bytes(), &KEYSTREAM);
    assert!(equals::<LEN>(&OBFSTRING, &KEYSTREAM, STRING.as_bytes()));
}

#[test]
fn test_obfstr_let() {
    obfstr! {
        let abc = "abc";
        let def = "defdef";
    }
    assert_eq!(abc, "abc");
    assert_eq!(def, "defdef");
}

#[test]
fn test_obfstr_const() {
    assert_eq!(obfstr!("\u{20}\0"), " \0");
    assert_eq!(obfstr!("\"\n\t\\\'\""), "\"\n\t\\\'\"");

    const LONG_STRING: &str =
        "This literal is very very very long to see if it correctly handles long strings";
    assert_eq!(obfstr!(LONG_STRING), LONG_STRING);

    const ABC: &str = "ABC";
    const WORLD: &str = "🌍";

    assert_eq!(obfbytes!(ABC.as_bytes()), "ABC".as_bytes());
    assert_eq!(obfbytes!(WORLD.as_bytes()), "🌍".as_bytes());
}