toilscript 0.1.16 → 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +843 -18
- package/dist/cli.js.map +2 -2
- package/dist/importmap.json +2 -2
- package/dist/web.js +3 -3
- package/package.json +1 -1
- package/std/assembly/bindings/dom.ts +2 -9
- package/std/assembly/bindings/webcrypto.ts +106 -0
- package/std/assembly/crypto/algorithms.ts +316 -0
- package/std/assembly/crypto/key.ts +38 -0
- package/std/assembly/crypto/subtle.ts +151 -0
- package/std/assembly/crypto.ts +142 -4
- package/std/assembly/index.d.ts +84 -1
package/std/assembly/crypto.ts
CHANGED
|
@@ -1,9 +1,147 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// Web Crypto for ToilScript — a synchronous SubtleCrypto plus ergonomic
|
|
2
|
+
// helpers, backed by metered host functions (see `bindings/webcrypto.ts` and
|
|
3
|
+
// the toil-backend `crypto` host module).
|
|
4
|
+
//
|
|
5
|
+
// Deviations from the web spec (ToilScript has no Promises): every method is
|
|
6
|
+
// synchronous and returns its value directly. Algorithm parameters are small
|
|
7
|
+
// classes (e.g. `AesGcmParams`) rather than object literals, and key usages are
|
|
8
|
+
// an i32 bitmask. RSA, on-host key generation, and the `jwk` format are not
|
|
9
|
+
// provided (RSA was dropped for an unfixable timing side-channel in the only
|
|
10
|
+
// pure-Rust implementation; keys are imported, never generated on-host).
|
|
11
|
+
|
|
12
|
+
import { webcrypto } from "bindings/webcrypto";
|
|
13
|
+
import { SubtleCrypto } from "crypto/subtle";
|
|
14
|
+
import { HmacImportParams, HmacParams, ALG_SHA_256, USAGE_SIGN } from "crypto/algorithms";
|
|
15
|
+
import { Encoding } from "encoding";
|
|
16
|
+
|
|
17
|
+
// Re-export the public surface so guests can import everything from "crypto".
|
|
18
|
+
export { SubtleCrypto } from "./crypto/subtle";
|
|
19
|
+
export { CryptoKey, CryptoKeyPair } from "./crypto/key";
|
|
20
|
+
export {
|
|
21
|
+
AlgorithmParams,
|
|
22
|
+
AesGcmParams,
|
|
23
|
+
AesCbcParams,
|
|
24
|
+
AesCtrParams,
|
|
25
|
+
HmacImportParams,
|
|
26
|
+
HmacParams,
|
|
27
|
+
Pbkdf2Params,
|
|
28
|
+
HkdfParams,
|
|
29
|
+
EcdsaParams,
|
|
30
|
+
EcKeyImportParams,
|
|
31
|
+
Ed25519Params,
|
|
32
|
+
X25519ImportParams,
|
|
33
|
+
EcdhParams,
|
|
34
|
+
algId,
|
|
35
|
+
algName,
|
|
36
|
+
formatId,
|
|
37
|
+
curveId,
|
|
38
|
+
cryptoError,
|
|
39
|
+
ALG_SHA_1,
|
|
40
|
+
ALG_SHA_256,
|
|
41
|
+
ALG_SHA_384,
|
|
42
|
+
ALG_SHA_512,
|
|
43
|
+
ALG_AES_GCM,
|
|
44
|
+
ALG_AES_CBC,
|
|
45
|
+
ALG_AES_CTR,
|
|
46
|
+
ALG_AES_KW,
|
|
47
|
+
ALG_HMAC,
|
|
48
|
+
ALG_ECDSA,
|
|
49
|
+
ALG_ED25519,
|
|
50
|
+
ALG_ECDH,
|
|
51
|
+
ALG_X25519,
|
|
52
|
+
ALG_HKDF,
|
|
53
|
+
ALG_PBKDF2,
|
|
54
|
+
CURVE_P256,
|
|
55
|
+
CURVE_P384,
|
|
56
|
+
USAGE_ENCRYPT,
|
|
57
|
+
USAGE_DECRYPT,
|
|
58
|
+
USAGE_SIGN,
|
|
59
|
+
USAGE_VERIFY,
|
|
60
|
+
USAGE_DERIVE_KEY,
|
|
61
|
+
USAGE_DERIVE_BITS,
|
|
62
|
+
USAGE_WRAP_KEY,
|
|
63
|
+
USAGE_UNWRAP_KEY,
|
|
64
|
+
} from "./crypto/algorithms";
|
|
65
|
+
|
|
66
|
+
function utf8Bytes(s: string): Uint8Array {
|
|
67
|
+
return Uint8Array.wrap(String.UTF8.encode(s));
|
|
68
|
+
}
|
|
4
69
|
|
|
5
70
|
export namespace crypto {
|
|
71
|
+
/// The synchronous SubtleCrypto singleton. `@lazy` defers initialization to
|
|
72
|
+
/// first use so accessing `crypto.subtle` from global scope (without an
|
|
73
|
+
/// explicit import of "crypto") doesn't hit a cross-module init-order error.
|
|
74
|
+
// @ts-ignore: decorator
|
|
75
|
+
@lazy export const subtle: SubtleCrypto = new SubtleCrypto();
|
|
76
|
+
|
|
77
|
+
/// Fill `array` with cryptographically strong random bytes.
|
|
6
78
|
export function getRandomValues(array: Uint8Array): void {
|
|
7
|
-
|
|
79
|
+
webcrypto.fillRandom(array.dataStart, array.byteLength);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/// An RFC 4122 version-4 UUID string.
|
|
83
|
+
export function randomUUID(): string {
|
|
84
|
+
let b = new Uint8Array(16);
|
|
85
|
+
webcrypto.randomUuid(b.dataStart);
|
|
86
|
+
b[6] = (b[6] & 0x0f) | 0x40; // version 4
|
|
87
|
+
b[8] = (b[8] & 0x3f) | 0x80; // variant 10
|
|
88
|
+
let h = toHex(b);
|
|
89
|
+
return (
|
|
90
|
+
h.substring(0, 8) +
|
|
91
|
+
"-" +
|
|
92
|
+
h.substring(8, 12) +
|
|
93
|
+
"-" +
|
|
94
|
+
h.substring(12, 16) +
|
|
95
|
+
"-" +
|
|
96
|
+
h.substring(16, 20) +
|
|
97
|
+
"-" +
|
|
98
|
+
h.substring(20, 32)
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// --- Ergonomic digest helpers (thin wrappers over subtle.digest) ----------
|
|
103
|
+
export function sha1(data: Uint8Array): Uint8Array {
|
|
104
|
+
return subtle.digest("SHA-1", data);
|
|
105
|
+
}
|
|
106
|
+
export function sha256(data: Uint8Array): Uint8Array {
|
|
107
|
+
return subtle.digest("SHA-256", data);
|
|
108
|
+
}
|
|
109
|
+
export function sha384(data: Uint8Array): Uint8Array {
|
|
110
|
+
return subtle.digest("SHA-384", data);
|
|
111
|
+
}
|
|
112
|
+
export function sha512(data: Uint8Array): Uint8Array {
|
|
113
|
+
return subtle.digest("SHA-512", data);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// String-input variants (UTF-8 encode, then hash).
|
|
117
|
+
export function sha1Text(s: string): Uint8Array {
|
|
118
|
+
return sha1(utf8Bytes(s));
|
|
119
|
+
}
|
|
120
|
+
export function sha256Text(s: string): Uint8Array {
|
|
121
|
+
return sha256(utf8Bytes(s));
|
|
122
|
+
}
|
|
123
|
+
export function sha384Text(s: string): Uint8Array {
|
|
124
|
+
return sha384(utf8Bytes(s));
|
|
125
|
+
}
|
|
126
|
+
export function sha512Text(s: string): Uint8Array {
|
|
127
|
+
return sha512(utf8Bytes(s));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/// One-shot HMAC-SHA-256 over raw key + message bytes.
|
|
131
|
+
export function hmacSha256(key: Uint8Array, msg: Uint8Array): Uint8Array {
|
|
132
|
+
let k = subtle.importKey("raw", key, new HmacImportParams(ALG_SHA_256), false, USAGE_SIGN);
|
|
133
|
+
return subtle.sign(new HmacParams(), k, msg);
|
|
134
|
+
}
|
|
135
|
+
export function hmacSha256Text(key: Uint8Array, msg: string): Uint8Array {
|
|
136
|
+
return hmacSha256(key, utf8Bytes(msg));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/// Lowercase hex string of `bytes` (handy for displaying digests).
|
|
140
|
+
export function toHex(bytes: Uint8Array): string {
|
|
141
|
+
let hexLen = bytes.length * 2;
|
|
142
|
+
if (hexLen == 0) return "";
|
|
143
|
+
let dst = new Uint8Array(hexLen);
|
|
144
|
+
Encoding.Hex.encode(bytes.dataStart, bytes.length, dst.dataStart);
|
|
145
|
+
return String.UTF8.decode(dst.buffer);
|
|
8
146
|
}
|
|
9
147
|
}
|
package/std/assembly/index.d.ts
CHANGED
|
@@ -2807,10 +2807,93 @@ declare namespace console {
|
|
|
2807
2807
|
export function timeEnd(label?: string): void;
|
|
2808
2808
|
}
|
|
2809
2809
|
|
|
2810
|
-
/**
|
|
2810
|
+
/** Opaque key handle for the Web Crypto API (per-request on the edge). */
|
|
2811
|
+
declare class CryptoKey {
|
|
2812
|
+
readonly handle: u32;
|
|
2813
|
+
readonly type: string;
|
|
2814
|
+
readonly extractable: bool;
|
|
2815
|
+
readonly algorithm: i32;
|
|
2816
|
+
readonly usages: i32;
|
|
2817
|
+
algorithmName(): string;
|
|
2818
|
+
hasUsage(u: i32): bool;
|
|
2819
|
+
}
|
|
2820
|
+
declare class CryptoKeyPair {
|
|
2821
|
+
readonly publicKey: CryptoKey;
|
|
2822
|
+
readonly privateKey: CryptoKey;
|
|
2823
|
+
}
|
|
2824
|
+
/** Base class for the algorithm-parameter objects passed to SubtleCrypto. */
|
|
2825
|
+
declare abstract class AlgorithmParams {}
|
|
2826
|
+
declare class AesGcmParams extends AlgorithmParams {
|
|
2827
|
+
constructor(iv: Uint8Array, additionalData?: Uint8Array, tagLength?: i32);
|
|
2828
|
+
}
|
|
2829
|
+
declare class AesCbcParams extends AlgorithmParams {
|
|
2830
|
+
constructor(iv: Uint8Array);
|
|
2831
|
+
}
|
|
2832
|
+
declare class AesCtrParams extends AlgorithmParams {
|
|
2833
|
+
constructor(counter: Uint8Array, length?: i32);
|
|
2834
|
+
}
|
|
2835
|
+
declare class HmacImportParams extends AlgorithmParams {
|
|
2836
|
+
constructor(hash: i32);
|
|
2837
|
+
}
|
|
2838
|
+
declare class HmacParams extends AlgorithmParams {}
|
|
2839
|
+
declare class Pbkdf2Params extends AlgorithmParams {
|
|
2840
|
+
constructor(hash: i32, salt: Uint8Array, iterations: u32);
|
|
2841
|
+
}
|
|
2842
|
+
declare class HkdfParams extends AlgorithmParams {
|
|
2843
|
+
constructor(hash: i32, salt: Uint8Array, info?: Uint8Array);
|
|
2844
|
+
}
|
|
2845
|
+
declare class EcdsaParams extends AlgorithmParams {
|
|
2846
|
+
constructor(hash: i32);
|
|
2847
|
+
}
|
|
2848
|
+
declare class EcKeyImportParams extends AlgorithmParams {
|
|
2849
|
+
constructor(alg: i32, namedCurve: i32);
|
|
2850
|
+
}
|
|
2851
|
+
declare class Ed25519Params extends AlgorithmParams {}
|
|
2852
|
+
declare class X25519ImportParams extends AlgorithmParams {}
|
|
2853
|
+
declare class EcdhParams extends AlgorithmParams {
|
|
2854
|
+
constructor(alg: i32, publicKeyHandle: i32);
|
|
2855
|
+
}
|
|
2856
|
+
/** Synchronous SubtleCrypto (no Promises). Returns values directly. */
|
|
2857
|
+
declare class SubtleCrypto {
|
|
2858
|
+
digest(algorithm: string, data: Uint8Array): Uint8Array;
|
|
2859
|
+
importKey(format: string, keyData: Uint8Array, algorithm: AlgorithmParams, extractable: bool, usages: i32): CryptoKey;
|
|
2860
|
+
exportKey(format: string, key: CryptoKey): Uint8Array;
|
|
2861
|
+
encrypt(algorithm: AlgorithmParams, key: CryptoKey, data: Uint8Array): Uint8Array;
|
|
2862
|
+
decrypt(algorithm: AlgorithmParams, key: CryptoKey, data: Uint8Array): Uint8Array;
|
|
2863
|
+
sign(algorithm: AlgorithmParams, key: CryptoKey, data: Uint8Array): Uint8Array;
|
|
2864
|
+
verify(algorithm: AlgorithmParams, key: CryptoKey, signature: Uint8Array, data: Uint8Array): bool;
|
|
2865
|
+
deriveBits(algorithm: AlgorithmParams, baseKey: CryptoKey, length: i32): Uint8Array;
|
|
2866
|
+
deriveKey(algorithm: AlgorithmParams, baseKey: CryptoKey, lengthBits: i32, derivedKeyAlgorithm: AlgorithmParams, extractable: bool, usages: i32): CryptoKey;
|
|
2867
|
+
}
|
|
2868
|
+
|
|
2869
|
+
// Algorithm / format / curve / usage ids (the Web Crypto ABI contract).
|
|
2870
|
+
declare const ALG_SHA_1: i32, ALG_SHA_256: i32, ALG_SHA_384: i32, ALG_SHA_512: i32;
|
|
2871
|
+
declare const ALG_AES_GCM: i32, ALG_AES_CBC: i32, ALG_AES_CTR: i32, ALG_AES_KW: i32;
|
|
2872
|
+
declare const ALG_HMAC: i32, ALG_ECDSA: i32, ALG_ED25519: i32, ALG_ECDH: i32, ALG_X25519: i32, ALG_HKDF: i32, ALG_PBKDF2: i32;
|
|
2873
|
+
declare const CURVE_P256: i32, CURVE_P384: i32;
|
|
2874
|
+
declare const USAGE_ENCRYPT: i32, USAGE_DECRYPT: i32, USAGE_SIGN: i32, USAGE_VERIFY: i32;
|
|
2875
|
+
declare const USAGE_DERIVE_KEY: i32, USAGE_DERIVE_BITS: i32, USAGE_WRAP_KEY: i32, USAGE_UNWRAP_KEY: i32;
|
|
2876
|
+
|
|
2877
|
+
/** Browser-like Web Crypto (synchronous SubtleCrypto + ergonomic helpers). */
|
|
2811
2878
|
declare namespace crypto {
|
|
2879
|
+
/** The synchronous SubtleCrypto instance. */
|
|
2880
|
+
export const subtle: SubtleCrypto;
|
|
2812
2881
|
/** Fills `array` with cryptographically strong random values. */
|
|
2813
2882
|
export function getRandomValues(array: Uint8Array): void;
|
|
2883
|
+
/** An RFC 4122 version-4 UUID string. */
|
|
2884
|
+
export function randomUUID(): string;
|
|
2885
|
+
export function sha1(data: Uint8Array): Uint8Array;
|
|
2886
|
+
export function sha256(data: Uint8Array): Uint8Array;
|
|
2887
|
+
export function sha384(data: Uint8Array): Uint8Array;
|
|
2888
|
+
export function sha512(data: Uint8Array): Uint8Array;
|
|
2889
|
+
export function sha1Text(s: string): Uint8Array;
|
|
2890
|
+
export function sha256Text(s: string): Uint8Array;
|
|
2891
|
+
export function sha384Text(s: string): Uint8Array;
|
|
2892
|
+
export function sha512Text(s: string): Uint8Array;
|
|
2893
|
+
export function hmacSha256(key: Uint8Array, msg: Uint8Array): Uint8Array;
|
|
2894
|
+
export function hmacSha256Text(key: Uint8Array, msg: string): Uint8Array;
|
|
2895
|
+
/** Lowercase hex string of `bytes`. */
|
|
2896
|
+
export function toHex(bytes: Uint8Array): string;
|
|
2814
2897
|
}
|
|
2815
2898
|
|
|
2816
2899
|
// Decorators
|