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
#[macro_use]
extern crate lazy_static;
extern crate proc_macro;
extern crate proc_macro2;
extern crate quote;
#[cfg(test)]
#[macro_use(expect)]
extern crate expectest;
use proc_macro::{TokenStream, TokenTree};
use proc_macro2::Literal;
use quote::quote;
use std::env;
use std::sync::{Arc, Mutex};
mod xor;
lazy_static! {
static ref MAGIC_SPELL: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None));
}
#[proc_macro]
pub fn use_litcrypt(tokens: TokenStream) -> TokenStream {
let magic_spell = env::var("LITCRYPT_ENCRYPT_KEY").ok().or_else(|| {
tokens
.into_iter()
.find(|a| matches!(a, TokenTree::Literal(_)))
.map(|a| match a {
TokenTree::Literal(lit) => {
let s = lit.to_string();
String::from(&s[1..s.len() - 1])
}
_ => "default-secret-word".to_string(),
})
});
{
let mut m_spell = MAGIC_SPELL.lock().unwrap();
*m_spell = magic_spell.clone();
}
let encdec_func = quote! {
pub mod litcrypt_internal {
pub fn xor(source: &[u8], key: &[u8]) -> Vec<u8> {
match key.len() {
0 => source.into(),
1 => xor_with_byte(source, key[0]),
_ => {
let key_iter = InfiniteByteIterator::new(key);
source.iter().zip(key_iter).map(|(&a, b)| a ^ b).collect()
}
}
}
pub fn xor_with_byte(source: &[u8], byte: u8) -> Vec<u8> {
source.iter().map(|&a| a ^ byte).collect()
}
struct InfiniteByteIterator<'a> {
bytes: &'a [u8],
index: usize,
}
impl<'a> InfiniteByteIterator<'a> {
pub fn new(bytes: &'a [u8]) -> InfiniteByteIterator<'a> {
InfiniteByteIterator {
bytes: bytes,
index: 0,
}
}
}
impl<'a> Iterator for InfiniteByteIterator<'a> {
type Item = u8;
fn next(&mut self) -> Option<u8> {
let byte = self.bytes[self.index];
self.index = next_index(self.index, self.bytes.len());
Some(byte)
}
}
fn next_index(index: usize, count: usize) -> usize {
if index + 1 < count {
index + 1
} else {
0
}
}
pub fn decrypt_bytes(encrypted: &[u8], encrypt_key: &[u8]) -> String {
let decrypted = xor(&encrypted[..], &encrypt_key);
String::from_utf8(decrypted).unwrap()
}
}
};
let result = if let Some(ekey) = magic_spell {
let ekey = xor::xor(ekey.as_bytes(), b"l33t");
let ekey = Literal::byte_string(&ekey);
quote! {
static LITCRYPT_ENCRYPT_KEY: &'static [u8] = #ekey;
#encdec_func
}
} else {
let ekey = xor::xor(b"default-secret-word", b"l33t");
let ekey = Literal::byte_string(&ekey);
quote! {
static LITCRYPT_ENCRYPT_KEY: &'static [u8] = #ekey;
#encdec_func
}
};
result.into()
}
#[proc_macro]
pub fn lc(_item: TokenStream) -> TokenStream {
let mut something = String::from("");
for tok in _item {
something = match tok {
TokenTree::Literal(lit) => lit.to_string(),
_ => "<unknown>".to_owned(),
}
}
something = String::from(&something[1..something.len() - 1]);
let ekey = {
let m_spell = MAGIC_SPELL.lock().unwrap();
(*m_spell).clone()
};
let encrypt_key = match ekey {
Some(ref a) => a.as_bytes(),
None => b"default-secret-word",
};
let encrypt_key = xor::xor(encrypt_key, b"l33t");
let encrypted = xor::xor(&something.as_bytes(), &encrypt_key);
let encrypted = Literal::byte_string(&encrypted);
let result = quote! {
crate::litcrypt_internal::decrypt_bytes(#encrypted, crate::LITCRYPT_ENCRYPT_KEY)
};
result.into()
}