The host API
What a codec cannot do well inside the sandbox, the host does. Native codecs
import these calls from the WebAssembly module pulzr:codec/v1; JavaScript
codecs see them as the pulzr global.
| JavaScript | Native import | Algorithms | Test vectors |
|---|---|---|---|
pulzr.md5(b), pulzr.sha1(b), pulzr.sha256(b) |
hash(alg, data, len) |
MD5 (1), SHA-1 (2), SHA-256 (3) | RFC 1321 suite, FIPS 180-2 A and B |
pulzr.hmac(alg, key, b) |
hmac(alg, key, kl, data, len) |
'md5', 'sha1', 'sha256' |
RFC 2202, RFC 4231 |
pulzr.encrypt(c, b), pulzr.decrypt(c, b) |
cipher(...) |
AES-128/192/256 in ECB, CBC, CTR; DES; 3DES; PKCS#7 padding | FIPS-197 C, SP 800-38A F.2.1 and F.5.1, FIPS 81, SP 800-67 |
the same, with mode: 'gcm' |
aead(...) |
AES-GCM, 12-byte nonce, associated data, the tag after the ciphertext | McGrew-Viega test cases 2, 4, 16 |
pulzr.base64.encode(b), .decode(s) |
base64(op, data, len) |
RFC 4648 | RFC 4648 section 10 |
pulzr.crc(name, b) |
crc(name, nl, data, len) |
The CRC catalogue’s names ('CRC-32', 'CRC-16/MODBUS', …) |
Each one’s check value of "123456789" |
pulzr.xml(text) |
xml(data, len) |
XML to a JSON tree: entities, CDATA, namespace prefixes | An ONVIF answer |
pulzr.emit(key, value) |
emit(key, kl, json, jl) |
A reading, for a native codec | |
pulzr.utf8.encode(s), .decode(b) |
The engine’s own, not a host call |
A cipher is described by an object:
var c = { alg: 'aes', mode: 'cbc', key: keyBytes, iv: ivBytes };var sealed = pulzr.encrypt(c, plainBytes);var gcm = { alg: 'aes', mode: 'gcm', key: keyBytes, iv: nonce12, aad: associatedBytes };Block modes pad with PKCS#7 unless the object says padding: false, when the
data must be whole blocks. A decrypt that does not open (a GCM tag that does
not match, bad padding) throws; a codec catches it and says why.
Bytes come back as a Uint8Array. A variable-length answer to a native call
is returned as its length; the module allocates that much and calls
take(dst) to receive it.
The rules
Section titled “The rules”- Pure. Deterministic: no I/O, no clock, no storage, no network. What
touches the world stays a request the caller makes, so replaying a command,
auditing it and running offline keep working. State goes through
memo. - Well specified, and expensive in the sandbox. A named algorithm or format, too slow or too large to carry in every codec. Conveniences (a byte reader, a JSON pointer) belong in a library.
- Versioned. A module imports the version it was written for, and one that imports a version or a call this host lacks is refused at load. A call never changes in place, and an algorithm’s number is never reused: a new one is added, and a host without it answers -1.
- Standard test vectors for every call, in
tests/vectors.rs.
A codec that needs something else can propose a host call.
Not in it, and why
Section titled “Not in it, and why”A clock or sleep |
Not pure. A codec waits by returning a request (or, for a library codec, a wait) |
| Inflate (gzip, deflate) | It qualifies, and no codec has needed it yet: propose it |
| A JSON-pointer or byte-reader helper | A convenience, for a library |