Skip to content

Commit fae91fd

Browse files
committed
feat: wasm working AES and obfuscation
1 parent b16193b commit fae91fd

File tree

16 files changed

+243
-88
lines changed

16 files changed

+243
-88
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ pkg
88
pkg.zip
99
.DS_Store
1010
crate/target
11-
crate/pkg
11+
crate/pkg
12+
crate-prod

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,18 @@ Names:
4949

5050
❌ Invent / Implement license mechanism
5151
- ✅ try webassembly
52-
- Cleanup rust code
53-
- cleanup rust deps
54-
- cleanup build chunks
55-
- add wasm files to package script
52+
- Cleanup rust code
53+
- cleanup rust deps
54+
- cleanup build chunks
55+
- add wasm files to package script
5656
- ❌ obfuscate wasm identifiers
57-
-
57+
- ❌ License
58+
- ability to seamless renewal
59+
- ideally can validate license with public key, but create with private key
60+
- non-ideally - both keys (or one common key) is used to create and validate
61+
- license is stored on device and not removed on update
62+
-
63+
- temp key: `dSgVkXp2s5v8y/B?`
5864

5965
❌ PoC / Implement vscode extension - mostly to understand how to license
6066

__tests__/search/codePatterns.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { search } from '/search'
22
import { compareCode } from '/astUtils';
33
import path from 'path'
44
import { getFilesList } from '/getFilesList'
5-
import { init } from '/wasm'
65
const filesList = getFilesList(path.resolve(__dirname, '__fixtures__'))
76

87
describe('code patterns', () => {

babel.plugins.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module.exports = function plugin() {
55
return {}
66
}
77

8+
const identifierReplacements = JSON.parse(process.env.BABEL_IDS_REPLACEMENTS)
9+
810
return {
911
visitor: {
1012
CallExpression(path, state) {
@@ -18,6 +20,20 @@ module.exports = function plugin() {
1820

1921
}
2022
},
23+
Identifier(path) {
24+
const replacement = identifierReplacements[path.node.name]
25+
if (typeof replacement === 'string') {
26+
console.log('Replaced identifier:', path.node.name, '=>', replacement)
27+
path.node.name = replacement
28+
}
29+
},
30+
StringLiteral(path) {
31+
if (path.node.value.includes('crate/pkg')) {
32+
path.node.value = path.node.value.replace('crate', 'crate-prod')
33+
console.log('Replaced crate import path to:', path.node.value)
34+
35+
}
36+
}
2137
},
2238
};
2339
}

crate/Cargo.lock

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crate/cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ serde = { version = "1.0", features = ["derive"] }
2525
# The `wasm-bindgen` crate provides the bare minimum functionality needed
2626
# to interact with JavaScript.
2727
wasm-bindgen = {version = "0.2.45", features = ["serde-serialize"] }
28-
2928
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
3029
# compared to the default allocator's ~10K. However, it is slower than the default
3130
# allocator, so it's not enabled by default.
3231
wee_alloc = { version = "0.4.2", optional = true }
3332
js-sys = "0.3.55"
33+
aes = "0.7.5"
34+
hex = "0.4.3"
3435
# The `web-sys` crate allows you to interact with the various browser APIs,
3536
# like the DOM.
3637
[dependencies.web-sys]

crate/src/aes_test.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use aes::cipher::{
2+
generic_array::GenericArray, BlockCipher, BlockDecrypt, BlockEncrypt, NewBlockCipher,
3+
};
4+
use aes::{Aes128, Block, ParBlocks};
5+
use wasm_bindgen::prelude::*;
6+
use web_sys::console;
7+
8+
pub fn aes_test() -> bool {
9+
// AES ECB
10+
let key = GenericArray::from_slice(&[
11+
100, 83, 103, 86, 107, 88, 112, 50, 115, 53, 118, 56, 121, 47, 66, 63,
12+
]);
13+
14+
let mut secret: [u8; 16] = Default::default();
15+
let secret_bytes = hex::decode("87bf66c78071ec9713178d30be52d1ae").unwrap();
16+
17+
secret.copy_from_slice(&secret_bytes[0..16]);
18+
let mut block = Block::from(secret);
19+
20+
// Initialize cipher
21+
let cipher = Aes128::new(&key);
22+
23+
// Decrypt block
24+
cipher.decrypt_block(&mut block);
25+
26+
let decoded_string = String::from_utf8(block.to_vec()).unwrap();
27+
28+
// console::log_1(&JsValue::from_str(&format!("decoded block: {:?}", block)));
29+
// console::log_1(&JsValue::from_str(&format!(
30+
// "decoded str: ${}$",
31+
// decoded_string
32+
// )));
33+
34+
// console::log_1(&JsValue::from_str(&format!(
35+
// "secret bytes: {:?}",
36+
// secret_bytes
37+
// )));
38+
return decoded_string.eq(&"hello world\u{5}\u{5}\u{5}\u{5}\u{5}".to_owned());
39+
}

crate/src/lib.rs

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use wasm_bindgen::prelude::*;
22
use web_sys::console;
33

4-
54
// When the `wee_alloc` feature is enabled, this uses `wee_alloc` as the global
65
// allocator.
76
//
@@ -10,10 +9,9 @@ use web_sys::console;
109
#[global_allocator]
1110
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
1211

13-
static mut ENABLED: &str = "strange_string";
14-
static arr: [i32; 5] = [1, 2, 3, 4, 5];
15-
static arr2: [&str;1] = ["dupa"];
12+
static mut ENABLED: bool = false;
1613

14+
mod aes_test;
1715
// This is like the `main` function, except for JavaScript.
1816
// This is executed on import (at least with current setup :v )
1917
#[wasm_bindgen(start)]
@@ -25,65 +23,27 @@ pub fn main_js() -> Result<(), JsValue> {
2523

2624
unsafe {
2725
// mock of license check on module load
28-
ENABLED = "yes";
26+
ENABLED = aes_test::aes_test();
2927
}
3028

31-
// Your code goes here!
32-
// console::log_1(&JsValue::from_str("Hello world!"));
33-
3429
Ok(())
3530
}
3631

37-
#[wasm_bindgen]
38-
pub fn enable(query:String) -> Result<(), JsValue> {
39-
unsafe {
40-
ENABLED = "yes";
41-
}
42-
Ok(())
43-
}
32+
use js_sys;
4433

4534
#[wasm_bindgen]
46-
pub fn get_str(query:String) -> Result<String, JsValue> {
47-
let res: String = "res: ".to_owned();
48-
let space: String = String::from(" ");
35+
pub fn trim_value(obj: &JsValue) -> Result<(), JsValue> {
4936
unsafe {
50-
// output = &(res + space + enabled + space + &query);
51-
Ok(res + &space + ENABLED + &space + &query + arr2[0] + &arr[4].to_string())
37+
assert_eq!(ENABLED, true);
5238
}
53-
}
54-
55-
use serde::{Serialize, Deserialize};
56-
57-
#[derive(Serialize, Deserialize)]
58-
struct Data {
59-
pub key:String
60-
}
61-
62-
#[wasm_bindgen]
63-
pub fn get_field(obj: &JsValue) -> Result<String, JsValue> {
64-
let example: Data = obj.into_serde().unwrap();
65-
Ok(example.key)
66-
}
67-
68-
use js_sys;
69-
70-
#[wasm_bindgen]
71-
pub fn set_field(obj: &JsValue) -> Result<(), JsValue> {
72-
js_sys::Reflect::set(&obj, &JsValue::from_str("key"), &JsValue::from_str("dupa"))?;
73-
74-
Ok(())
75-
}
76-
77-
#[wasm_bindgen]
78-
pub fn transform_value(obj: &JsValue) -> Result<(), JsValue> {
7939
let value = js_sys::Reflect::get(&obj, &JsValue::from_str("value"))?;
80-
let to_trim:String = value.into_serde().unwrap();
40+
let to_trim: String = value.into_serde().unwrap();
8141
let trimmed = to_trim.trim();
8242

83-
js_sys::Reflect::set(&obj, &JsValue::from_str("value"), &JsValue::from_str(&trimmed))?;
84-
// console::log_1(&JsValue::from_str(&format!("Trimmed: {} -> {}", to_trim, trimmed)));
85-
unsafe {
86-
assert_eq!(ENABLED, "yes");
87-
}
43+
js_sys::Reflect::set(
44+
&obj,
45+
&JsValue::from_str("value"),
46+
&JsValue::from_str(&trimmed),
47+
)?;
8848
Ok(())
89-
}
49+
}

createPkg.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ fs.writeFileSync(`${pkgDir}/package.json`, JSON.stringify({
2525

2626
fs.copyFileSync('./bin.js', `${pkgDir}/bin.js`)
2727
fs.copyFileSync('./dist/cli.js', `${distDir}/cli.js`)
28-
fs.copyFileSync('./dist/worker.js', `${distDir}/worker.js`)
28+
fs.copyFileSync('./dist/worker.js', `${distDir}/worker.js`)
29+
fs.copyFileSync('./dist/835.js', `${distDir}/835.js`)
30+
fs.copyFileSync('./dist/index_bg.wasm', `${distDir}/index_bg.wasm`)

0 commit comments

Comments
 (0)