@fangorn-network/sdk 2026.7.14 → 2026.7.24
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/lib/crypto/aes.js +1 -1
- package/lib/crypto/encryption.d.ts +2 -2
- package/lib/crypto/encryption.js +3 -3
- package/lib/engine/index.js +1 -1
- package/lib/fangorn.d.ts +1 -1
- package/lib/fangorn.js +2 -6
- package/lib/index.d.ts +2 -1
- package/lib/index.js +2 -1
- package/lib/providers/storage/car.js +1 -1
- package/lib/providers/storage/pinata.js +22 -3
- package/package.json +3 -1
- package/lib/node_modules/@noble/ciphers/esm/_polyval.js +0 -218
- package/lib/node_modules/@noble/ciphers/esm/aes.js +0 -261
- package/lib/node_modules/@noble/ciphers/esm/utils.js +0 -156
- package/lib/node_modules/@noble/curves/esm/abstract/curve.js +0 -299
- package/lib/node_modules/@noble/curves/esm/abstract/edwards.js +0 -532
- package/lib/node_modules/@noble/curves/esm/abstract/modular.js +0 -344
- package/lib/node_modules/@noble/curves/esm/abstract/montgomery.js +0 -137
- package/lib/node_modules/@noble/curves/esm/ed25519.js +0 -262
- package/lib/node_modules/@noble/curves/esm/utils.js +0 -150
package/lib/crypto/aes.js
CHANGED
|
@@ -118,7 +118,7 @@ declare function buildAccessRequest(params: {
|
|
|
118
118
|
/** Unix seconds; defaults to now. Must be inside the worker's TIMESTAMP_WINDOW. */
|
|
119
119
|
timestamp?: number;
|
|
120
120
|
}): Promise<AccessRequest>;
|
|
121
|
-
|
|
121
|
+
interface DecryptHandleParams {
|
|
122
122
|
handle: HandleFieldInput;
|
|
123
123
|
/** Signs the /access request; its address is what the worker checks settlement for. */
|
|
124
124
|
signer: AccessSigner;
|
|
@@ -126,7 +126,7 @@ type DecryptHandleParams = {
|
|
|
126
126
|
nullifier: Hex;
|
|
127
127
|
/** self-hkdf-v1 only: the same 32-byte secret used to seal. */
|
|
128
128
|
ownSecret?: Uint8Array;
|
|
129
|
-
}
|
|
129
|
+
}
|
|
130
130
|
/**
|
|
131
131
|
* Resolve a {@link HandleFieldInput} back to plaintext via the worker's
|
|
132
132
|
* settlement-gated `POST /access`.
|
package/lib/crypto/encryption.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { GCM_NONCE_LENGTH, aesGcmDecrypt, aesGcmEncrypt } from "./aes.js";
|
|
2
|
-
import { x25519 } from "../node_modules/@noble/curves/esm/ed25519.js";
|
|
3
2
|
import { getRandomValues } from "./rand.js";
|
|
4
3
|
import { bytesToHex, encodePacked, hexToBytes, keccak256 } from "viem";
|
|
4
|
+
import { x25519 } from "@noble/curves/ed25519";
|
|
5
5
|
import { sha256 } from "@noble/hashes/sha2.js";
|
|
6
6
|
import { hkdf } from "@noble/hashes/hkdf.js";
|
|
7
7
|
|
|
@@ -86,7 +86,7 @@ async function encryptAndUpload(params) {
|
|
|
86
86
|
},
|
|
87
87
|
body: ciphertext
|
|
88
88
|
});
|
|
89
|
-
if (!uploadRes.ok) throw new Error(`upload failed: ${uploadRes.status}`);
|
|
89
|
+
if (!uploadRes.ok) throw new Error(`upload failed: ${uploadRes.status.toString()}`);
|
|
90
90
|
const { objectKey } = await uploadRes.json();
|
|
91
91
|
return {
|
|
92
92
|
"@type": "handle",
|
|
@@ -152,7 +152,7 @@ async function decryptHandle(params) {
|
|
|
152
152
|
headers: { "Content-Type": "application/json" },
|
|
153
153
|
body: JSON.stringify(access)
|
|
154
154
|
});
|
|
155
|
-
if (!res.ok) throw new Error(`access failed: ${res.status}`);
|
|
155
|
+
if (!res.ok) throw new Error(`access failed: ${res.status.toString()}`);
|
|
156
156
|
const bytes = new Uint8Array(await res.arrayBuffer());
|
|
157
157
|
if (gadget === GADGET_SELF_HKDF_V1) {
|
|
158
158
|
if (!params.ownSecret) throw new Error(`${gadget} requires ownSecret to decrypt`);
|
package/lib/engine/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { buildPages, decodeBlock, encodeBlock, readPages } from "./graph.js";
|
|
2
1
|
import { packCar, readCar } from "./car.js";
|
|
2
|
+
import { buildPages, decodeBlock, encodeBlock, readPages } from "./graph.js";
|
|
3
3
|
import { CID } from "multiformats/cid";
|
|
4
4
|
import { sha256 } from "multiformats/hashes/sha2";
|
|
5
5
|
import * as Digest from "multiformats/hashes/digest";
|
package/lib/fangorn.d.ts
CHANGED
package/lib/fangorn.js
CHANGED
|
@@ -19,20 +19,16 @@ function resolveStorage(storage) {
|
|
|
19
19
|
var Fangorn = class Fangorn {
|
|
20
20
|
ctx;
|
|
21
21
|
_engine = null;
|
|
22
|
-
_metagraph =
|
|
22
|
+
_metagraph = new MetagraphRegistry();
|
|
23
23
|
constructor(ctx) {
|
|
24
24
|
this.ctx = ctx;
|
|
25
25
|
}
|
|
26
26
|
get engine() {
|
|
27
27
|
if (!this.ctx.metadataStorage) throw new Error("fangorn.engine requires storage configurations. Pass { pinata: { ... } } to Fangorn.create()");
|
|
28
|
-
|
|
29
|
-
this._metagraph = new MetagraphRegistry();
|
|
30
|
-
this._engine = new FangornEngine(this.ctx.metadataStorage, this._metagraph, this.ctx.dataRegistry);
|
|
31
|
-
}
|
|
28
|
+
this._engine ??= new FangornEngine(this.ctx.metadataStorage, this._metagraph, this.ctx.dataRegistry);
|
|
32
29
|
return this._engine;
|
|
33
30
|
}
|
|
34
31
|
get metagraph() {
|
|
35
|
-
if (!this._metagraph) throw new Error("MetagraphRegistry has not been initialized. Ensure the client is connected.");
|
|
36
32
|
return this._metagraph;
|
|
37
33
|
}
|
|
38
34
|
static create(options) {
|
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AppConfig, FangornConfig, SupportedNetworks } from "./config.js";
|
|
2
|
+
import { AccessRequest, AccessSigner, DecryptHandleParams, EncryptAndUploadParams, GADGET_SELF_HKDF_V1, GADGET_WORKER_USDC_V1, Gadget, HandleFieldInput, X25519_PUBKEY_LENGTH, accessMessageHash, buildAccessRequest, decryptHandle, encryptAndUpload, seal, sealSelf, sha256Hex, unseal, unsealSelf } from "./crypto/encryption.js";
|
|
2
3
|
import { Fangorn, NamespaceChange, SubscribeOptions } from "./fangorn.js";
|
|
3
4
|
import { AssetProcessor, BuildAssetGraphOptions, CommitFileGraph, Edge, FileFacade, ProcessorResult, Vertex, buildAssetGraph, buildMarkdownGraph, extractMarkdownLinks } from "./harness/index.js";
|
|
4
5
|
import { PinataBackend } from "./providers/storage/pinata.js";
|
|
5
6
|
import { deserialize, retrieveByCid, serialize } from "./providers/storage/utils.js";
|
|
6
|
-
export { AppConfig, AssetProcessor, BuildAssetGraphOptions, CommitFileGraph, Edge, Fangorn, FangornConfig, FileFacade, NamespaceChange, PinataBackend, ProcessorResult, SubscribeOptions, SupportedNetworks, Vertex, buildAssetGraph, buildMarkdownGraph, deserialize, extractMarkdownLinks, retrieveByCid, serialize };
|
|
7
|
+
export { AccessRequest, AccessSigner, AppConfig, AssetProcessor, BuildAssetGraphOptions, CommitFileGraph, DecryptHandleParams, Edge, EncryptAndUploadParams, Fangorn, FangornConfig, FileFacade, GADGET_SELF_HKDF_V1, GADGET_WORKER_USDC_V1, Gadget, HandleFieldInput, NamespaceChange, PinataBackend, ProcessorResult, SubscribeOptions, SupportedNetworks, Vertex, X25519_PUBKEY_LENGTH, accessMessageHash, buildAccessRequest, buildAssetGraph, buildMarkdownGraph, decryptHandle, deserialize, encryptAndUpload, extractMarkdownLinks, retrieveByCid, seal, sealSelf, serialize, sha256Hex, unseal, unsealSelf };
|
package/lib/index.js
CHANGED
|
@@ -3,5 +3,6 @@ import { deserialize, retrieveByCid, serialize } from "./providers/storage/utils
|
|
|
3
3
|
import { PinataBackend } from "./providers/storage/pinata.js";
|
|
4
4
|
import { Fangorn } from "./fangorn.js";
|
|
5
5
|
import { buildAssetGraph, buildMarkdownGraph, extractMarkdownLinks } from "./harness/index.js";
|
|
6
|
+
import { GADGET_SELF_HKDF_V1, GADGET_WORKER_USDC_V1, X25519_PUBKEY_LENGTH, accessMessageHash, buildAccessRequest, decryptHandle, encryptAndUpload, seal, sealSelf, sha256Hex, unseal, unsealSelf } from "./crypto/encryption.js";
|
|
6
7
|
|
|
7
|
-
export { Fangorn, FangornConfig, PinataBackend, SupportedNetworks, buildAssetGraph, buildMarkdownGraph, deserialize, extractMarkdownLinks, retrieveByCid, serialize };
|
|
8
|
+
export { Fangorn, FangornConfig, GADGET_SELF_HKDF_V1, GADGET_WORKER_USDC_V1, PinataBackend, SupportedNetworks, X25519_PUBKEY_LENGTH, accessMessageHash, buildAccessRequest, buildAssetGraph, buildMarkdownGraph, decryptHandle, deserialize, encryptAndUpload, extractMarkdownLinks, retrieveByCid, seal, sealSelf, serialize, sha256Hex, unseal, unsealSelf };
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { retrieveByCid, serialize } from "./utils.js";
|
|
2
|
-
import { packCar } from "
|
|
2
|
+
import { packCar } from "../../engine/car.js";
|
|
3
3
|
import { keccak256, stringToBytes } from "viem";
|
|
4
4
|
import { CID } from "multiformats/cid";
|
|
5
|
+
import { encode } from "multiformats/block";
|
|
5
6
|
import { PinataSDK } from "pinata";
|
|
6
7
|
import * as dagCbor from "@ipld/dag-cbor";
|
|
8
|
+
import { sha256 } from "multiformats/hashes/sha2";
|
|
7
9
|
|
|
8
10
|
//#region src/providers/storage/pinata.ts
|
|
9
11
|
const RAW_CODE = 85;
|
|
@@ -66,9 +68,26 @@ var PinataBackend = class {
|
|
|
66
68
|
*/
|
|
67
69
|
async putMany(items) {
|
|
68
70
|
if (items.length === 0) return {};
|
|
69
|
-
const
|
|
71
|
+
const blocks = [];
|
|
72
|
+
const uriByName = {};
|
|
73
|
+
for (const item of items) {
|
|
74
|
+
const block = await encode({
|
|
75
|
+
value: item.data,
|
|
76
|
+
codec: dagCbor,
|
|
77
|
+
hasher: sha256
|
|
78
|
+
});
|
|
79
|
+
blocks.push(block);
|
|
80
|
+
uriByName[item.name] = `ipfs://${block.cid.toString()}`;
|
|
81
|
+
}
|
|
82
|
+
const rootBlock = await encode({
|
|
83
|
+
value: uriByName,
|
|
84
|
+
codec: dagCbor,
|
|
85
|
+
hasher: sha256
|
|
86
|
+
});
|
|
87
|
+
blocks.push(rootBlock);
|
|
88
|
+
const bytes = await packCar(rootBlock.cid, blocks);
|
|
70
89
|
await withUploadRetry(`car[${items.length.toString()}f/${(bytes.length / 1e6).toFixed(1)}MB]`, async () => {
|
|
71
|
-
const file = new File([bytes], "data.car", { type: "application/vnd.ipld.car" });
|
|
90
|
+
const file = new File([bytes.buffer], "data.car", { type: "application/vnd.ipld.car" });
|
|
72
91
|
await this.pinata.upload.public.file(file).car();
|
|
73
92
|
});
|
|
74
93
|
return uriByName;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fangorn-network/sdk",
|
|
3
|
-
"version": "2026.07.
|
|
3
|
+
"version": "2026.07.24",
|
|
4
4
|
"description": "Git for knowledge graphs",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -61,6 +61,8 @@
|
|
|
61
61
|
"@ipld/car": "^5.4.6",
|
|
62
62
|
"@ipld/dag-cbor": "^10.0.1",
|
|
63
63
|
"@ipld/unixfs": "^3.0.0",
|
|
64
|
+
"@noble/ciphers": "^1.3.0",
|
|
65
|
+
"@noble/curves": "^1.9.7",
|
|
64
66
|
"@noble/hashes": "^2.2.0",
|
|
65
67
|
"@noir-lang/noir_js": "1.0.0-beta.22",
|
|
66
68
|
"@semaphore-protocol/group": "^4.14.2",
|
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
import { abytes, aexists, aoutput, clean, copyBytes, createView, toBytes, u32 } from "./utils.js";
|
|
2
|
-
|
|
3
|
-
//#region node_modules/@noble/ciphers/esm/_polyval.js
|
|
4
|
-
/**
|
|
5
|
-
* GHash from AES-GCM and its little-endian "mirror image" Polyval from AES-SIV.
|
|
6
|
-
*
|
|
7
|
-
* Implemented in terms of GHash with conversion function for keys
|
|
8
|
-
* GCM GHASH from
|
|
9
|
-
* [NIST SP800-38d](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf),
|
|
10
|
-
* SIV from
|
|
11
|
-
* [RFC 8452](https://datatracker.ietf.org/doc/html/rfc8452).
|
|
12
|
-
*
|
|
13
|
-
* GHASH modulo: x^128 + x^7 + x^2 + x + 1
|
|
14
|
-
* POLYVAL modulo: x^128 + x^127 + x^126 + x^121 + 1
|
|
15
|
-
*
|
|
16
|
-
* @module
|
|
17
|
-
*/
|
|
18
|
-
const BLOCK_SIZE = 16;
|
|
19
|
-
const ZEROS16 = /* @__PURE__ */ new Uint8Array(16);
|
|
20
|
-
const ZEROS32 = u32(ZEROS16);
|
|
21
|
-
const POLY = 225;
|
|
22
|
-
const mul2 = (s0, s1, s2, s3) => {
|
|
23
|
-
const hiBit = s3 & 1;
|
|
24
|
-
return {
|
|
25
|
-
s3: s2 << 31 | s3 >>> 1,
|
|
26
|
-
s2: s1 << 31 | s2 >>> 1,
|
|
27
|
-
s1: s0 << 31 | s1 >>> 1,
|
|
28
|
-
s0: s0 >>> 1 ^ POLY << 24 & -(hiBit & 1)
|
|
29
|
-
};
|
|
30
|
-
};
|
|
31
|
-
const swapLE = (n) => (n >>> 0 & 255) << 24 | (n >>> 8 & 255) << 16 | (n >>> 16 & 255) << 8 | n >>> 24 & 255 | 0;
|
|
32
|
-
/**
|
|
33
|
-
* `mulX_POLYVAL(ByteReverse(H))` from spec
|
|
34
|
-
* @param k mutated in place
|
|
35
|
-
*/
|
|
36
|
-
function _toGHASHKey(k) {
|
|
37
|
-
k.reverse();
|
|
38
|
-
const hiBit = k[15] & 1;
|
|
39
|
-
let carry = 0;
|
|
40
|
-
for (let i = 0; i < k.length; i++) {
|
|
41
|
-
const t = k[i];
|
|
42
|
-
k[i] = t >>> 1 | carry;
|
|
43
|
-
carry = (t & 1) << 7;
|
|
44
|
-
}
|
|
45
|
-
k[0] ^= -hiBit & 225;
|
|
46
|
-
return k;
|
|
47
|
-
}
|
|
48
|
-
const estimateWindow = (bytes) => {
|
|
49
|
-
if (bytes > 64 * 1024) return 8;
|
|
50
|
-
if (bytes > 1024) return 4;
|
|
51
|
-
return 2;
|
|
52
|
-
};
|
|
53
|
-
var GHASH = class {
|
|
54
|
-
constructor(key, expectedLength) {
|
|
55
|
-
this.blockLen = BLOCK_SIZE;
|
|
56
|
-
this.outputLen = BLOCK_SIZE;
|
|
57
|
-
this.s0 = 0;
|
|
58
|
-
this.s1 = 0;
|
|
59
|
-
this.s2 = 0;
|
|
60
|
-
this.s3 = 0;
|
|
61
|
-
this.finished = false;
|
|
62
|
-
key = toBytes(key);
|
|
63
|
-
abytes(key, 16);
|
|
64
|
-
const kView = createView(key);
|
|
65
|
-
let k0 = kView.getUint32(0, false);
|
|
66
|
-
let k1 = kView.getUint32(4, false);
|
|
67
|
-
let k2 = kView.getUint32(8, false);
|
|
68
|
-
let k3 = kView.getUint32(12, false);
|
|
69
|
-
const doubles = [];
|
|
70
|
-
for (let i = 0; i < 128; i++) {
|
|
71
|
-
doubles.push({
|
|
72
|
-
s0: swapLE(k0),
|
|
73
|
-
s1: swapLE(k1),
|
|
74
|
-
s2: swapLE(k2),
|
|
75
|
-
s3: swapLE(k3)
|
|
76
|
-
});
|
|
77
|
-
({s0: k0, s1: k1, s2: k2, s3: k3} = mul2(k0, k1, k2, k3));
|
|
78
|
-
}
|
|
79
|
-
const W = estimateWindow(expectedLength || 1024);
|
|
80
|
-
if (![
|
|
81
|
-
1,
|
|
82
|
-
2,
|
|
83
|
-
4,
|
|
84
|
-
8
|
|
85
|
-
].includes(W)) throw new Error("ghash: invalid window size, expected 2, 4 or 8");
|
|
86
|
-
this.W = W;
|
|
87
|
-
const windows = 128 / W;
|
|
88
|
-
const windowSize = this.windowSize = 2 ** W;
|
|
89
|
-
const items = [];
|
|
90
|
-
for (let w = 0; w < windows; w++) for (let byte = 0; byte < windowSize; byte++) {
|
|
91
|
-
let s0 = 0, s1 = 0, s2 = 0, s3 = 0;
|
|
92
|
-
for (let j = 0; j < W; j++) {
|
|
93
|
-
if (!(byte >>> W - j - 1 & 1)) continue;
|
|
94
|
-
const { s0: d0, s1: d1, s2: d2, s3: d3 } = doubles[W * w + j];
|
|
95
|
-
s0 ^= d0, s1 ^= d1, s2 ^= d2, s3 ^= d3;
|
|
96
|
-
}
|
|
97
|
-
items.push({
|
|
98
|
-
s0,
|
|
99
|
-
s1,
|
|
100
|
-
s2,
|
|
101
|
-
s3
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
this.t = items;
|
|
105
|
-
}
|
|
106
|
-
_updateBlock(s0, s1, s2, s3) {
|
|
107
|
-
s0 ^= this.s0, s1 ^= this.s1, s2 ^= this.s2, s3 ^= this.s3;
|
|
108
|
-
const { W, t, windowSize } = this;
|
|
109
|
-
let o0 = 0, o1 = 0, o2 = 0, o3 = 0;
|
|
110
|
-
const mask = (1 << W) - 1;
|
|
111
|
-
let w = 0;
|
|
112
|
-
for (const num of [
|
|
113
|
-
s0,
|
|
114
|
-
s1,
|
|
115
|
-
s2,
|
|
116
|
-
s3
|
|
117
|
-
]) for (let bytePos = 0; bytePos < 4; bytePos++) {
|
|
118
|
-
const byte = num >>> 8 * bytePos & 255;
|
|
119
|
-
for (let bitPos = 8 / W - 1; bitPos >= 0; bitPos--) {
|
|
120
|
-
const bit = byte >>> W * bitPos & mask;
|
|
121
|
-
const { s0: e0, s1: e1, s2: e2, s3: e3 } = t[w * windowSize + bit];
|
|
122
|
-
o0 ^= e0, o1 ^= e1, o2 ^= e2, o3 ^= e3;
|
|
123
|
-
w += 1;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
this.s0 = o0;
|
|
127
|
-
this.s1 = o1;
|
|
128
|
-
this.s2 = o2;
|
|
129
|
-
this.s3 = o3;
|
|
130
|
-
}
|
|
131
|
-
update(data) {
|
|
132
|
-
aexists(this);
|
|
133
|
-
data = toBytes(data);
|
|
134
|
-
abytes(data);
|
|
135
|
-
const b32 = u32(data);
|
|
136
|
-
const blocks = Math.floor(data.length / BLOCK_SIZE);
|
|
137
|
-
const left = data.length % BLOCK_SIZE;
|
|
138
|
-
for (let i = 0; i < blocks; i++) this._updateBlock(b32[i * 4 + 0], b32[i * 4 + 1], b32[i * 4 + 2], b32[i * 4 + 3]);
|
|
139
|
-
if (left) {
|
|
140
|
-
ZEROS16.set(data.subarray(blocks * BLOCK_SIZE));
|
|
141
|
-
this._updateBlock(ZEROS32[0], ZEROS32[1], ZEROS32[2], ZEROS32[3]);
|
|
142
|
-
clean(ZEROS32);
|
|
143
|
-
}
|
|
144
|
-
return this;
|
|
145
|
-
}
|
|
146
|
-
destroy() {
|
|
147
|
-
const { t } = this;
|
|
148
|
-
for (const elm of t) elm.s0 = 0, elm.s1 = 0, elm.s2 = 0, elm.s3 = 0;
|
|
149
|
-
}
|
|
150
|
-
digestInto(out) {
|
|
151
|
-
aexists(this);
|
|
152
|
-
aoutput(out, this);
|
|
153
|
-
this.finished = true;
|
|
154
|
-
const { s0, s1, s2, s3 } = this;
|
|
155
|
-
const o32 = u32(out);
|
|
156
|
-
o32[0] = s0;
|
|
157
|
-
o32[1] = s1;
|
|
158
|
-
o32[2] = s2;
|
|
159
|
-
o32[3] = s3;
|
|
160
|
-
return out;
|
|
161
|
-
}
|
|
162
|
-
digest() {
|
|
163
|
-
const res = new Uint8Array(BLOCK_SIZE);
|
|
164
|
-
this.digestInto(res);
|
|
165
|
-
this.destroy();
|
|
166
|
-
return res;
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
var Polyval = class extends GHASH {
|
|
170
|
-
constructor(key, expectedLength) {
|
|
171
|
-
key = toBytes(key);
|
|
172
|
-
abytes(key);
|
|
173
|
-
const ghKey = _toGHASHKey(copyBytes(key));
|
|
174
|
-
super(ghKey, expectedLength);
|
|
175
|
-
clean(ghKey);
|
|
176
|
-
}
|
|
177
|
-
update(data) {
|
|
178
|
-
data = toBytes(data);
|
|
179
|
-
aexists(this);
|
|
180
|
-
const b32 = u32(data);
|
|
181
|
-
const left = data.length % BLOCK_SIZE;
|
|
182
|
-
const blocks = Math.floor(data.length / BLOCK_SIZE);
|
|
183
|
-
for (let i = 0; i < blocks; i++) this._updateBlock(swapLE(b32[i * 4 + 3]), swapLE(b32[i * 4 + 2]), swapLE(b32[i * 4 + 1]), swapLE(b32[i * 4 + 0]));
|
|
184
|
-
if (left) {
|
|
185
|
-
ZEROS16.set(data.subarray(blocks * BLOCK_SIZE));
|
|
186
|
-
this._updateBlock(swapLE(ZEROS32[3]), swapLE(ZEROS32[2]), swapLE(ZEROS32[1]), swapLE(ZEROS32[0]));
|
|
187
|
-
clean(ZEROS32);
|
|
188
|
-
}
|
|
189
|
-
return this;
|
|
190
|
-
}
|
|
191
|
-
digestInto(out) {
|
|
192
|
-
aexists(this);
|
|
193
|
-
aoutput(out, this);
|
|
194
|
-
this.finished = true;
|
|
195
|
-
const { s0, s1, s2, s3 } = this;
|
|
196
|
-
const o32 = u32(out);
|
|
197
|
-
o32[0] = s0;
|
|
198
|
-
o32[1] = s1;
|
|
199
|
-
o32[2] = s2;
|
|
200
|
-
o32[3] = s3;
|
|
201
|
-
return out.reverse();
|
|
202
|
-
}
|
|
203
|
-
};
|
|
204
|
-
function wrapConstructorWithKey(hashCons) {
|
|
205
|
-
const hashC = (msg, key) => hashCons(key, msg.length).update(toBytes(msg)).digest();
|
|
206
|
-
const tmp = hashCons(new Uint8Array(16), 0);
|
|
207
|
-
hashC.outputLen = tmp.outputLen;
|
|
208
|
-
hashC.blockLen = tmp.blockLen;
|
|
209
|
-
hashC.create = (key, expectedLength) => hashCons(key, expectedLength);
|
|
210
|
-
return hashC;
|
|
211
|
-
}
|
|
212
|
-
/** GHash MAC for AES-GCM. */
|
|
213
|
-
const ghash = wrapConstructorWithKey((key, expectedLength) => new GHASH(key, expectedLength));
|
|
214
|
-
/** Polyval MAC for AES-SIV. */
|
|
215
|
-
const polyval = wrapConstructorWithKey((key, expectedLength) => new Polyval(key, expectedLength));
|
|
216
|
-
|
|
217
|
-
//#endregion
|
|
218
|
-
export { ghash };
|
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
import { abytes, clean, copyBytes, createView, equalBytes, getOutput, isAligned32, setBigUint64, u32, u64Lengths, u8, wrapCipher } from "./utils.js";
|
|
2
|
-
import { ghash } from "./_polyval.js";
|
|
3
|
-
|
|
4
|
-
//#region node_modules/@noble/ciphers/esm/aes.js
|
|
5
|
-
/**
|
|
6
|
-
* [AES](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard)
|
|
7
|
-
* a.k.a. Advanced Encryption Standard
|
|
8
|
-
* is a variant of Rijndael block cipher, standardized by NIST in 2001.
|
|
9
|
-
* We provide the fastest available pure JS implementation.
|
|
10
|
-
*
|
|
11
|
-
* Data is split into 128-bit blocks. Encrypted in 10/12/14 rounds (128/192/256 bits). In every round:
|
|
12
|
-
* 1. **S-box**, table substitution
|
|
13
|
-
* 2. **Shift rows**, cyclic shift left of all rows of data array
|
|
14
|
-
* 3. **Mix columns**, multiplying every column by fixed polynomial
|
|
15
|
-
* 4. **Add round key**, round_key xor i-th column of array
|
|
16
|
-
*
|
|
17
|
-
* Check out [FIPS-197](https://csrc.nist.gov/files/pubs/fips/197/final/docs/fips-197.pdf)
|
|
18
|
-
* and [original proposal](https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/aes-development/rijndael-ammended.pdf)
|
|
19
|
-
* @module
|
|
20
|
-
*/
|
|
21
|
-
const BLOCK_SIZE = 16;
|
|
22
|
-
const BLOCK_SIZE32 = 4;
|
|
23
|
-
const EMPTY_BLOCK = /* @__PURE__ */ new Uint8Array(BLOCK_SIZE);
|
|
24
|
-
const POLY = 283;
|
|
25
|
-
function mul2(n) {
|
|
26
|
-
return n << 1 ^ POLY & -(n >> 7);
|
|
27
|
-
}
|
|
28
|
-
function mul(a, b) {
|
|
29
|
-
let res = 0;
|
|
30
|
-
for (; b > 0; b >>= 1) {
|
|
31
|
-
res ^= a & -(b & 1);
|
|
32
|
-
a = mul2(a);
|
|
33
|
-
}
|
|
34
|
-
return res;
|
|
35
|
-
}
|
|
36
|
-
const sbox = /* @__PURE__ */ (() => {
|
|
37
|
-
const t = new Uint8Array(256);
|
|
38
|
-
for (let i = 0, x = 1; i < 256; i++, x ^= mul2(x)) t[i] = x;
|
|
39
|
-
const box = new Uint8Array(256);
|
|
40
|
-
box[0] = 99;
|
|
41
|
-
for (let i = 0; i < 255; i++) {
|
|
42
|
-
let x = t[255 - i];
|
|
43
|
-
x |= x << 8;
|
|
44
|
-
box[t[i]] = (x ^ x >> 4 ^ x >> 5 ^ x >> 6 ^ x >> 7 ^ 99) & 255;
|
|
45
|
-
}
|
|
46
|
-
clean(t);
|
|
47
|
-
return box;
|
|
48
|
-
})();
|
|
49
|
-
const rotr32_8 = (n) => n << 24 | n >>> 8;
|
|
50
|
-
const rotl32_8 = (n) => n << 8 | n >>> 24;
|
|
51
|
-
function genTtable(sbox$1, fn) {
|
|
52
|
-
if (sbox$1.length !== 256) throw new Error("Wrong sbox length");
|
|
53
|
-
const T0 = new Uint32Array(256).map((_, j) => fn(sbox$1[j]));
|
|
54
|
-
const T1 = T0.map(rotl32_8);
|
|
55
|
-
const T2 = T1.map(rotl32_8);
|
|
56
|
-
const T3 = T2.map(rotl32_8);
|
|
57
|
-
const T01 = new Uint32Array(256 * 256);
|
|
58
|
-
const T23 = new Uint32Array(256 * 256);
|
|
59
|
-
const sbox2 = new Uint16Array(256 * 256);
|
|
60
|
-
for (let i = 0; i < 256; i++) for (let j = 0; j < 256; j++) {
|
|
61
|
-
const idx = i * 256 + j;
|
|
62
|
-
T01[idx] = T0[i] ^ T1[j];
|
|
63
|
-
T23[idx] = T2[i] ^ T3[j];
|
|
64
|
-
sbox2[idx] = sbox$1[i] << 8 | sbox$1[j];
|
|
65
|
-
}
|
|
66
|
-
return {
|
|
67
|
-
sbox: sbox$1,
|
|
68
|
-
sbox2,
|
|
69
|
-
T0,
|
|
70
|
-
T1,
|
|
71
|
-
T2,
|
|
72
|
-
T3,
|
|
73
|
-
T01,
|
|
74
|
-
T23
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
const tableEncoding = /* @__PURE__ */ genTtable(sbox, (s) => mul(s, 3) << 24 | s << 16 | s << 8 | mul(s, 2));
|
|
78
|
-
const xPowers = /* @__PURE__ */ (() => {
|
|
79
|
-
const p = new Uint8Array(16);
|
|
80
|
-
for (let i = 0, x = 1; i < 16; i++, x = mul2(x)) p[i] = x;
|
|
81
|
-
return p;
|
|
82
|
-
})();
|
|
83
|
-
/** Key expansion used in CTR. */
|
|
84
|
-
function expandKeyLE(key) {
|
|
85
|
-
abytes(key);
|
|
86
|
-
const len = key.length;
|
|
87
|
-
if (![
|
|
88
|
-
16,
|
|
89
|
-
24,
|
|
90
|
-
32
|
|
91
|
-
].includes(len)) throw new Error("aes: invalid key size, should be 16, 24 or 32, got " + len);
|
|
92
|
-
const { sbox2 } = tableEncoding;
|
|
93
|
-
const toClean = [];
|
|
94
|
-
if (!isAligned32(key)) toClean.push(key = copyBytes(key));
|
|
95
|
-
const k32 = u32(key);
|
|
96
|
-
const Nk = k32.length;
|
|
97
|
-
const subByte = (n) => applySbox(sbox2, n, n, n, n);
|
|
98
|
-
const xk = new Uint32Array(len + 28);
|
|
99
|
-
xk.set(k32);
|
|
100
|
-
for (let i = Nk; i < xk.length; i++) {
|
|
101
|
-
let t = xk[i - 1];
|
|
102
|
-
if (i % Nk === 0) t = subByte(rotr32_8(t)) ^ xPowers[i / Nk - 1];
|
|
103
|
-
else if (Nk > 6 && i % Nk === 4) t = subByte(t);
|
|
104
|
-
xk[i] = xk[i - Nk] ^ t;
|
|
105
|
-
}
|
|
106
|
-
clean(...toClean);
|
|
107
|
-
return xk;
|
|
108
|
-
}
|
|
109
|
-
function apply0123(T01, T23, s0, s1, s2, s3) {
|
|
110
|
-
return T01[s0 << 8 & 65280 | s1 >>> 8 & 255] ^ T23[s2 >>> 8 & 65280 | s3 >>> 24 & 255];
|
|
111
|
-
}
|
|
112
|
-
function applySbox(sbox2, s0, s1, s2, s3) {
|
|
113
|
-
return sbox2[s0 & 255 | s1 & 65280] | sbox2[s2 >>> 16 & 255 | s3 >>> 16 & 65280] << 16;
|
|
114
|
-
}
|
|
115
|
-
function encrypt(xk, s0, s1, s2, s3) {
|
|
116
|
-
const { sbox2, T01, T23 } = tableEncoding;
|
|
117
|
-
let k = 0;
|
|
118
|
-
s0 ^= xk[k++], s1 ^= xk[k++], s2 ^= xk[k++], s3 ^= xk[k++];
|
|
119
|
-
const rounds = xk.length / 4 - 2;
|
|
120
|
-
for (let i = 0; i < rounds; i++) {
|
|
121
|
-
const t0 = xk[k++] ^ apply0123(T01, T23, s0, s1, s2, s3);
|
|
122
|
-
const t1 = xk[k++] ^ apply0123(T01, T23, s1, s2, s3, s0);
|
|
123
|
-
const t2 = xk[k++] ^ apply0123(T01, T23, s2, s3, s0, s1);
|
|
124
|
-
const t3 = xk[k++] ^ apply0123(T01, T23, s3, s0, s1, s2);
|
|
125
|
-
s0 = t0, s1 = t1, s2 = t2, s3 = t3;
|
|
126
|
-
}
|
|
127
|
-
return {
|
|
128
|
-
s0: xk[k++] ^ applySbox(sbox2, s0, s1, s2, s3),
|
|
129
|
-
s1: xk[k++] ^ applySbox(sbox2, s1, s2, s3, s0),
|
|
130
|
-
s2: xk[k++] ^ applySbox(sbox2, s2, s3, s0, s1),
|
|
131
|
-
s3: xk[k++] ^ applySbox(sbox2, s3, s0, s1, s2)
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
function ctr32(xk, isLE, nonce, src, dst) {
|
|
135
|
-
abytes(nonce, BLOCK_SIZE);
|
|
136
|
-
abytes(src);
|
|
137
|
-
dst = getOutput(src.length, dst);
|
|
138
|
-
const ctr = nonce;
|
|
139
|
-
const c32 = u32(ctr);
|
|
140
|
-
const view = createView(ctr);
|
|
141
|
-
const src32 = u32(src);
|
|
142
|
-
const dst32 = u32(dst);
|
|
143
|
-
const ctrPos = isLE ? 0 : 12;
|
|
144
|
-
const srcLen = src.length;
|
|
145
|
-
let ctrNum = view.getUint32(ctrPos, isLE);
|
|
146
|
-
let { s0, s1, s2, s3 } = encrypt(xk, c32[0], c32[1], c32[2], c32[3]);
|
|
147
|
-
for (let i = 0; i + 4 <= src32.length; i += 4) {
|
|
148
|
-
dst32[i + 0] = src32[i + 0] ^ s0;
|
|
149
|
-
dst32[i + 1] = src32[i + 1] ^ s1;
|
|
150
|
-
dst32[i + 2] = src32[i + 2] ^ s2;
|
|
151
|
-
dst32[i + 3] = src32[i + 3] ^ s3;
|
|
152
|
-
ctrNum = ctrNum + 1 >>> 0;
|
|
153
|
-
view.setUint32(ctrPos, ctrNum, isLE);
|
|
154
|
-
({s0, s1, s2, s3} = encrypt(xk, c32[0], c32[1], c32[2], c32[3]));
|
|
155
|
-
}
|
|
156
|
-
const start = BLOCK_SIZE * Math.floor(src32.length / BLOCK_SIZE32);
|
|
157
|
-
if (start < srcLen) {
|
|
158
|
-
const b32 = new Uint32Array([
|
|
159
|
-
s0,
|
|
160
|
-
s1,
|
|
161
|
-
s2,
|
|
162
|
-
s3
|
|
163
|
-
]);
|
|
164
|
-
const buf = u8(b32);
|
|
165
|
-
for (let i = start, pos = 0; i < srcLen; i++, pos++) dst[i] = src[i] ^ buf[pos];
|
|
166
|
-
clean(b32);
|
|
167
|
-
}
|
|
168
|
-
return dst;
|
|
169
|
-
}
|
|
170
|
-
function computeTag(fn, isLE, key, data, AAD) {
|
|
171
|
-
const aadLength = AAD ? AAD.length : 0;
|
|
172
|
-
const h = fn.create(key, data.length + aadLength);
|
|
173
|
-
if (AAD) h.update(AAD);
|
|
174
|
-
const num = u64Lengths(8 * data.length, 8 * aadLength, isLE);
|
|
175
|
-
h.update(data);
|
|
176
|
-
h.update(num);
|
|
177
|
-
const res = h.digest();
|
|
178
|
-
clean(num);
|
|
179
|
-
return res;
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* GCM: Galois/Counter Mode.
|
|
183
|
-
* Modern, parallel version of CTR, with MAC.
|
|
184
|
-
* Be careful: MACs can be forged.
|
|
185
|
-
* Unsafe to use random nonces under the same key, due to collision chance.
|
|
186
|
-
* As for nonce size, prefer 12-byte, instead of 8-byte.
|
|
187
|
-
*/
|
|
188
|
-
const gcm = /* @__PURE__ */ wrapCipher({
|
|
189
|
-
blockSize: 16,
|
|
190
|
-
nonceLength: 12,
|
|
191
|
-
tagLength: 16,
|
|
192
|
-
varSizeNonce: true
|
|
193
|
-
}, function aesgcm(key, nonce, AAD) {
|
|
194
|
-
if (nonce.length < 8) throw new Error("aes/gcm: invalid nonce length");
|
|
195
|
-
const tagLength = 16;
|
|
196
|
-
function _computeTag(authKey, tagMask, data) {
|
|
197
|
-
const tag = computeTag(ghash, false, authKey, data, AAD);
|
|
198
|
-
for (let i = 0; i < tagMask.length; i++) tag[i] ^= tagMask[i];
|
|
199
|
-
return tag;
|
|
200
|
-
}
|
|
201
|
-
function deriveKeys() {
|
|
202
|
-
const xk = expandKeyLE(key);
|
|
203
|
-
const authKey = EMPTY_BLOCK.slice();
|
|
204
|
-
const counter = EMPTY_BLOCK.slice();
|
|
205
|
-
ctr32(xk, false, counter, counter, authKey);
|
|
206
|
-
if (nonce.length === 12) counter.set(nonce);
|
|
207
|
-
else {
|
|
208
|
-
const nonceLen = EMPTY_BLOCK.slice();
|
|
209
|
-
setBigUint64(createView(nonceLen), 8, BigInt(nonce.length * 8), false);
|
|
210
|
-
const g = ghash.create(authKey).update(nonce).update(nonceLen);
|
|
211
|
-
g.digestInto(counter);
|
|
212
|
-
g.destroy();
|
|
213
|
-
}
|
|
214
|
-
return {
|
|
215
|
-
xk,
|
|
216
|
-
authKey,
|
|
217
|
-
counter,
|
|
218
|
-
tagMask: ctr32(xk, false, counter, EMPTY_BLOCK)
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
return {
|
|
222
|
-
encrypt(plaintext) {
|
|
223
|
-
const { xk, authKey, counter, tagMask } = deriveKeys();
|
|
224
|
-
const out = new Uint8Array(plaintext.length + tagLength);
|
|
225
|
-
const toClean = [
|
|
226
|
-
xk,
|
|
227
|
-
authKey,
|
|
228
|
-
counter,
|
|
229
|
-
tagMask
|
|
230
|
-
];
|
|
231
|
-
if (!isAligned32(plaintext)) toClean.push(plaintext = copyBytes(plaintext));
|
|
232
|
-
ctr32(xk, false, counter, plaintext, out.subarray(0, plaintext.length));
|
|
233
|
-
const tag = _computeTag(authKey, tagMask, out.subarray(0, out.length - tagLength));
|
|
234
|
-
toClean.push(tag);
|
|
235
|
-
out.set(tag, plaintext.length);
|
|
236
|
-
clean(...toClean);
|
|
237
|
-
return out;
|
|
238
|
-
},
|
|
239
|
-
decrypt(ciphertext) {
|
|
240
|
-
const { xk, authKey, counter, tagMask } = deriveKeys();
|
|
241
|
-
const toClean = [
|
|
242
|
-
xk,
|
|
243
|
-
authKey,
|
|
244
|
-
tagMask,
|
|
245
|
-
counter
|
|
246
|
-
];
|
|
247
|
-
if (!isAligned32(ciphertext)) toClean.push(ciphertext = copyBytes(ciphertext));
|
|
248
|
-
const data = ciphertext.subarray(0, -tagLength);
|
|
249
|
-
const passedTag = ciphertext.subarray(-tagLength);
|
|
250
|
-
const tag = _computeTag(authKey, tagMask, data);
|
|
251
|
-
toClean.push(tag);
|
|
252
|
-
if (!equalBytes(tag, passedTag)) throw new Error("aes/gcm: invalid ghash tag");
|
|
253
|
-
const out = ctr32(xk, false, counter, data);
|
|
254
|
-
clean(...toClean);
|
|
255
|
-
return out;
|
|
256
|
-
}
|
|
257
|
-
};
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
//#endregion
|
|
261
|
-
export { gcm };
|