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
//! # Boo
//!
//! Boo encrypts literal data in the final binary, at compile time, preventing static analysis tools from
//! reading values.
//!
//! # Usage
//!
//! Add the dependency:
//!
//! ```toml
//! [dependencies]
//! boo-rs = "0.1"
//! ```
//!
//! Set an optional encryption key (or fallback to a 64-byte randomly generated one):
//!
//! ```bash
//! export BOO_KEY="secret-key"
//! ```
//!
//! # Example
//!
//! ```rust
//! extern crate alloc;
//! #[macro_use]
//! extern crate boo_rs;
//!
//! boo_init!();
//!
//! #[allow(unused_variables)]
//! fn main() {
//! let n = boo!(3);
//! let text = boo!("hello");
//! let bytes = boo!(b"\x01\x02\x03");
//! let pair = boo!(("host", 443));
//! let nested = boo!([[1, 2], [3, 4]]);
//! }
//! ```
//!
//! `boo_init!()` must be called once before using the `boo!()` macro.
//! After that, the macro can be used anywhere to encrypt almost all Rust literal values.
//!
//! # Performance
//!
//! Decryption happens on the stack. The cost is O(n), where n is the length of the data in bytes.
//!
//! - All decrypted types except `String` and `CStr` are stored on the stack without performance overhead.
//! - `&str` and `&CStr` decryption are stored into their heap-allocated variants.
//! - Special case: binary strings are decrypted into owned `[u8]` arrays.
extern crate alloc;
extern crate core;
extern crate proc_macro;
extern crate proc_macro2;
extern crate quote;
extern crate rand;
extern crate syn;
use fs;
use ;
use LazyLock;
use Literal;
use quote;
use ;
use crateBranch;
use crateLiteralBytes;
const INCLUDE_ERROR: &str = r#"expected one file path (ex. "data.txt")"#;
/// Cryptographic key
static KEY: = new;
/// Initialize the boo library allowing use of the boo macros.
///
/// Optionally set a custom key using the `BOO_KEY` environment variable.
/// Fallbacks to a random 64-bytes cryptographic key.
///
/// # Important
///
/// `boo_init!()` must be called once before using the `boo!()` macros.
///
/// # Example
///
/// ```
/// # extern crate alloc;
/// # #[macro_use] extern crate boo_rs;
/// boo_init!();
/// ```
/// Encrypts a literal
///
/// The embedded ciphertext carries a sibling checksum, verified at runtime before decryption:
/// this catches a direct edit to the ciphertext bytes in the compiled binary (the checksum
/// constant is a separate embedded value the edit doesn't touch), not an edit to the decrypted
/// plaintext or to code that reads it, and not a patch that also recomputes this checksum.
///
/// # Performance
///
/// Decryption happens on the stack. The cost is O(n), where n is the length of the data in bytes.
///
/// All decrypted types except `String` and `CStr` are stored on the stack without performance overhead.
///
/// # Panics
///
/// At macro-expansion time, panics if the input isn't a supported literal. At runtime, panics
/// if the embedded ciphertext no longer matches its embedded checksum.
///
/// # Returns
///
/// The same value that is passed in
///
/// # Example
///
/// ```
/// # extern crate alloc;
/// # #[macro_use] extern crate boo_rs;
/// boo_init!();
/// fn main() {
/// assert_eq!(boo!("boo"), "boo");
/// }
/// ```
/// Branches on a boolean condition through an opaque-predicate dispatch instead of a plain
/// `if`/`else`.
///
/// Raises the cost of spotting the branch in source review or a static disassembler's xrefs: the
/// dispatch is an XOR-masked comparison salted per call site, not a direct test of the condition,
/// hidden from the optimizer via [`core::hint::black_box`] to keep a release build from folding
/// it back to a plain branch. This still offers no resistance to a live debugger stepping through
/// the code - same limits as the crate's literal encryption.
///
/// # Panics
///
/// Panics if the input isn't `cond { .. } else { .. }`. `else if` chains aren't supported.
///
/// # Example
///
/// ```
/// # extern crate alloc;
/// # #[macro_use] extern crate boo_rs;
/// # boo_init!();
/// let licensed = true;
/// assert_eq!(boo_branch!(licensed { "full" } else { "trial" }), "full");
/// ```
/// Publishes `Mangled`, mangling a fixed-size value against this process's cookie and a fresh
/// per-value nonce for storage.
///
/// Mirrors glibc's `PTR_MANGLE`/Windows' `EncodePointer`: masks a fixed-size value against a
/// cookie generated once at process start, folded per value with a nonce - no two mangled values
/// share a keystream. A value a live memory-editing tool saves in one run decodes to garbage after
/// a restart - see `mangle.rs` for the mechanism.
///
/// `Mangled` is an ordinary type from here on: `Mangled::new(value)` to store, and
/// `mangled.reveal_with(|plain| ..)` to read it back - the plaintext never outlives that closure.
/// No further macro involved on either path; this one only exists to paste a private copy of the
/// mangling code into the calling crate, keeping its symbol private to each dependent crate.
///
/// # Important
///
/// `boo_mangle_init!()` must be called once, at the crate root, before using `Mangled`.
///
/// # Example
///
/// ```
/// # extern crate alloc;
/// # #[macro_use] extern crate boo_rs;
/// # boo_init!();
/// boo_mangle_init!();
///
/// fn main() {
/// let original = [1u8, 2, 3, 4];
/// let mangled = Mangled::new(original);
/// mangled.reveal_with(|plain| assert_eq!(*plain, original));
/// }
/// ```
/// Per-invocation salt derived from this macro call's line/column and the process-wide [`KEY`].
///
/// Never uses `file!()`: a source path embedded in the salt would leak into the compiled binary.
/// Encrypts a raw file as bytes
///
/// # Performance
///
/// Decryption happens on the stack. The cost is O(n), where n is the length of the data in bytes.
///
/// # Returns
///
/// File content as bytes
///
/// # Example
///
/// ```ignore
/// # extern crate alloc;
/// # #[macro_use] extern crate boo_rs;
/// # boo_init!();
/// let my_file = boo_include_bytes!("my-file.txt");
/// ```
/// Encrypts a UTF-8 file as a string
///
/// # Performance
///
/// Decryption happens on the stack. The cost is O(n), where n is the length of the data in bytes.
///
/// # Returns
///
/// File content as string
///
/// # Example
///
/// ```ignore
/// # extern crate alloc;
/// # #[macro_use] extern crate boo_rs;
/// # boo_init!();
/// let my_file = boo_include_str!("my-file.txt");
/// ```
/// Encrypts raw file from the path specified by an environment variable
///
/// # Performance
///
/// Decryption happens on the stack. The cost is O(n), where n is the length of the data in bytes.
///
/// # Returns
///
/// File content as bytes
///
/// # Example
///
/// ```ignore
/// # extern crate alloc;
/// # #[macro_use] extern crate boo_rs;
/// # boo_init!();
/// // Requires ENV_FILE to be set to an existing path
/// let my_file = boo_include_env!("ENV_FILE");
/// ```
/// Reads a single string literal from a token stream
///
/// # Arguments
///
/// * `tokens` - Token stream containing a single string literal
/// Makes a path relative to the calling source code file