-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c73102a
commit 63b8871
Showing
9 changed files
with
191 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
// eslint-disable-next-line @typescript-eslint/triple-slash-reference | ||
/// <reference types="emscripten" /> | ||
|
||
declare namespace mod { | ||
export function inflate (data: Uint8Array, len: number): Uint8Array | ||
export function aesEnc (data: Uint8Array, key: Uint8Array): Uint8Array | ||
export function _wz_zlib_inflate (source: number, srclen: number, dest: number, destlen: number): number | ||
export function _wz_aes_ecb_encrypt (data: number, dataLen: number, key: number, out: number, outLen: number): number | ||
export function _malloc (size: number): number | ||
export function _free (ptr: number): void | ||
} | ||
|
||
declare function init (moduleOverrides?: Partial<EmscriptenModule>): Promise<{ Module: typeof mod }> | ||
|
||
export declare const inflate: typeof mod.inflate | ||
export declare const aesEnc: typeof mod.aesEnc | ||
export declare function inflate (data: Uint8Array, len: number): Uint8Array | ||
export declare function aesEnc (data: Uint8Array, key: Uint8Array): Uint8Array | ||
|
||
export default init |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,69 @@ | ||
exports.inflate = function (data, len) { | ||
return Module.inflate(data, len) | ||
var srclen = data.length >>> 0; | ||
var source = Module._malloc(srclen); | ||
if (!source) throw new Error('malloc failed'); | ||
var memory = new Uint8Array(Module.HEAPU8.buffer, source, srclen); | ||
memory.set(data); | ||
|
||
var destlen = len >>> 0; | ||
var dest = Module._malloc(destlen); | ||
if (!dest) { | ||
Module._free(source); | ||
throw new Error('malloc failed'); | ||
} | ||
var r = Module._wz_zlib_inflate(source, srclen, dest, destlen); | ||
Module._free(source); | ||
if (r === 0) { | ||
var ret = new Uint8Array(Module.HEAPU8.buffer, dest, destlen).slice(); | ||
Module._free(dest); | ||
return ret; | ||
} | ||
Module._free(dest); | ||
throw new Error('Inflate failed'); | ||
} | ||
|
||
exports.aesEnc = function (data, key) { | ||
return Module.aesEnc(data, key) | ||
if (key.length !== 32) { | ||
throw new Error('Invalid key'); | ||
} | ||
|
||
var srclen = data.length >>> 0; | ||
var source = Module._malloc(srclen); | ||
if (!source) throw new Error('malloc failed'); | ||
var srcMemory = new Uint8Array(Module.HEAPU8.buffer, source, srclen); | ||
srcMemory.set(data); | ||
|
||
var outLenPointer = Module._malloc(4); | ||
if (!source) { | ||
Module._free(source); | ||
throw new Error('malloc failed'); | ||
} | ||
Module._wz_aes_ecb_encrypt(source, srclen, 0, 0, outLenPointer); | ||
|
||
var outLen = Module.HEAPU32[outLenPointer >> 2] | ||
var out = Module._malloc(outLen); | ||
if (!out) { | ||
Module._free(source); | ||
Module._free(outLenPointer); | ||
throw new Error('malloc failed'); | ||
} | ||
var keyLen = key.length >>> 0; | ||
var keyPointer = Module._malloc(keyLen); | ||
if (!keyPointer) { | ||
Module._free(source); | ||
Module._free(outLenPointer); | ||
Module._free(out); | ||
throw new Error('malloc failed'); | ||
} | ||
var keyMemory = new Uint8Array(Module.HEAPU8.buffer, keyPointer, keyLen); | ||
keyMemory.set(key); | ||
|
||
Module._wz_aes_ecb_encrypt(source, srclen, keyPointer, out, outLenPointer); | ||
|
||
var ret = new Uint8Array(Module.HEAPU8.buffer, out, outLen).slice(); | ||
Module._free(source); | ||
Module._free(outLenPointer); | ||
Module._free(out); | ||
Module._free(keyPointer); | ||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <stddef.h> | ||
#include <stdlib.h> | ||
#include <stdint.h> | ||
#include <string.h> | ||
#include <emscripten.h> | ||
#include "zlib.h" | ||
#include "openssl/aes.h" | ||
|
||
EMSCRIPTEN_KEEPALIVE | ||
int wz_zlib_inflate(uint8_t *source, | ||
size_t srclen, | ||
uint8_t *dest, | ||
size_t destlen) { | ||
z_stream infstream; | ||
infstream.zalloc = Z_NULL; | ||
infstream.zfree = Z_NULL; | ||
infstream.opaque = Z_NULL; | ||
infstream.avail_in = (uInt) srclen; // size of input | ||
infstream.next_in = (Bytef*) source; // input char array NOLINT | ||
infstream.avail_out = (uInt) destlen; // size of output | ||
infstream.next_out = (Bytef*) dest; // output char array NOLINT | ||
|
||
inflateInit(&infstream); | ||
int r = inflate(&infstream, Z_NO_FLUSH); | ||
inflateEnd(&infstream); | ||
return r; | ||
} | ||
|
||
EMSCRIPTEN_KEEPALIVE | ||
int wz_aes_ecb_encrypt(const uint8_t* data, | ||
size_t data_len, | ||
const uint8_t* key, | ||
uint8_t* out, | ||
size_t* out_len) { | ||
if (data == NULL || out_len == NULL) { | ||
return 1; | ||
} | ||
|
||
uint8_t* data_buf = NULL; | ||
|
||
size_t padding = data_len % 16; | ||
size_t encrypt_len = 0; | ||
if (padding != 0) { | ||
padding = 16 - padding; | ||
encrypt_len = data_len + padding; | ||
if (out == NULL) { | ||
*out_len = encrypt_len; | ||
return 0; | ||
} | ||
if (key == NULL) return 2; | ||
data_buf = (uint8_t*) malloc(encrypt_len); // NOLINT | ||
memcpy(data_buf, data, data_len); | ||
memset(data_buf + data_len, padding, padding); | ||
} else { | ||
encrypt_len = data_len; | ||
if (out == NULL) { | ||
*out_len = encrypt_len; | ||
return 0; | ||
} | ||
if (key == NULL) return 2; | ||
data_buf = (uint8_t*) malloc(encrypt_len); // NOLINT= | ||
memcpy(data_buf, data, data_len); | ||
} | ||
|
||
AES_KEY k; | ||
AES_set_encrypt_key(key, 256, &k); | ||
AES_ecb_encrypt(data_buf, out, &k, AES_ENCRYPT); | ||
|
||
free(data_buf); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.