Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion StarlingMonkey
Submodule StarlingMonkey updated 128 files
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"dependencies": {
"@bytecodealliance/jco": "^1.15.1",
"@bytecodealliance/preview2-shim": "^0.29.0",
"@bytecodealliance/weval": "^0.5.0",
"@bytecodealliance/weval": "^0.5.1",
"@bytecodealliance/wizer": "^10.0.0",
"es-module-lexer": "^1.6.0",
"oxc-parser": "^0.154.0"
Expand Down
65 changes: 65 additions & 0 deletions test/cases/crypto-subtle/source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const encoder = new TextEncoder();
const decoder = new TextDecoder();

function toHex(buffer) {
return Array.from(new Uint8Array(buffer), (b) =>
b.toString(16).padStart(2, '0'),
).join('');
}

function fromHex(hex) {
const out = new Uint8Array(hex.length / 2);
for (let i = 0; i < out.length; i++) {
out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
}
return out;
}

function importAesKey(key, usage) {
return crypto.subtle.importKey('raw', key, { name: 'AES-CBC' }, false, [
usage,
]);
}

export const exports = {
async pbkdf2Sha256(password, salt, iterations, length) {
const key = await crypto.subtle.importKey(
'raw',
encoder.encode(password),
'PBKDF2',
false,
['deriveBits'],
);
const bits = await crypto.subtle.deriveBits(
{
name: 'PBKDF2',
hash: 'SHA-256',
salt: encoder.encode(salt),
iterations,
},
key,
length,
);
return toHex(bits);
},

async aesCbcEncrypt(key, iv, plaintext) {
const cryptoKey = await importAesKey(key, 'encrypt');
const ciphertext = await crypto.subtle.encrypt(
{ name: 'AES-CBC', iv },
cryptoKey,
encoder.encode(plaintext),
);
return toHex(ciphertext);
},

async aesCbcDecrypt(key, iv, ciphertext) {
const cryptoKey = await importAesKey(key, 'decrypt');
const plaintext = await crypto.subtle.decrypt(
{ name: 'AES-CBC', iv },
cryptoKey,
fromHex(ciphertext),
);
return decoder.decode(plaintext);
},
};
25 changes: 25 additions & 0 deletions test/cases/crypto-subtle/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { strictEqual } from 'node:assert';

// Expected values computed with Node's crypto.pbkdf2Sync / createCipheriv.
const PBKDF2_SHA256_PASSWORD_SALT_1000_256 =
'632c2812e46d4604102ba7618e9d6d7d2f8128f6266b4a03264d2a0460b7dcb3';

const AES_KEY = Uint8Array.from({ length: 32 }, (_, i) => i);
const AES_IV = Uint8Array.from({ length: 16 }, (_, i) => 0xf0 + i);
const PLAINTEXT = 'ComponentizeJS on StarlingMonkey v0.5.0';
const AES_256_CBC_CIPHERTEXT =
'd6bf9b3184d17b8c5c442baea2bedf72562f2f644911d6f30ff228bbec182b1d' +
'feca06a5be162d2a41ea85e29b152867';

export function test(instance) {
const { exports } = instance;

strictEqual(
exports.pbkdf2Sha256('password', 'salt', 1000, 256),
PBKDF2_SHA256_PASSWORD_SALT_1000_256,
);

const ciphertext = exports.aesCbcEncrypt(AES_KEY, AES_IV, PLAINTEXT);
strictEqual(ciphertext, AES_256_CBC_CIPHERTEXT);
strictEqual(exports.aesCbcDecrypt(AES_KEY, AES_IV, ciphertext), PLAINTEXT);
}
9 changes: 9 additions & 0 deletions test/cases/crypto-subtle/world.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package local:crypto-subtle;

world crypto-subtle {
export exports: interface {
pbkdf2-sha256: func(password: string, salt: string, iterations: u32, length: u32) -> string;
aes-cbc-encrypt: func(key: list<u8>, iv: list<u8>, plaintext: string) -> string;
aes-cbc-decrypt: func(key: list<u8>, iv: list<u8>, ciphertext: string) -> string;
}
}