@trebired/crypto 0.0.0-stage → 0.2.0
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/.trebired/logger/config.ts +4 -0
- package/CHANGELOG.md +19 -0
- package/LICENSE +21 -0
- package/README.md +156 -2
- package/SPEC.md +179 -0
- package/dist/basics.d.ts +7 -0
- package/dist/basics.d.ts.map +1 -0
- package/dist/basics.js +17 -0
- package/dist/basics.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +135 -0
- package/dist/cli.js.map +1 -0
- package/dist/errors.d.ts +9 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +12 -0
- package/dist/errors.js.map +1 -0
- package/dist/files/files.d.ts +56 -0
- package/dist/files/files.d.ts.map +1 -0
- package/dist/files/files.js +117 -0
- package/dist/files/files.js.map +1 -0
- package/dist/files/format.d.ts +43 -0
- package/dist/files/format.d.ts.map +1 -0
- package/dist/files/format.js +82 -0
- package/dist/files/format.js.map +1 -0
- package/dist/files/slots.d.ts +20 -0
- package/dist/files/slots.d.ts.map +1 -0
- package/dist/files/slots.js +125 -0
- package/dist/files/slots.js.map +1 -0
- package/dist/files/streams.d.ts +18 -0
- package/dist/files/streams.d.ts.map +1 -0
- package/dist/files/streams.js +140 -0
- package/dist/files/streams.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/keys/env.d.ts +17 -0
- package/dist/keys/env.d.ts.map +1 -0
- package/dist/keys/env.js +99 -0
- package/dist/keys/env.js.map +1 -0
- package/dist/keys/ring.d.ts +13 -0
- package/dist/keys/ring.d.ts.map +1 -0
- package/dist/keys/ring.js +66 -0
- package/dist/keys/ring.js.map +1 -0
- package/dist/logging.d.ts +3 -0
- package/dist/logging.d.ts.map +1 -0
- package/dist/logging.js +7 -0
- package/dist/logging.js.map +1 -0
- package/dist/messages.d.ts +9 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +29 -0
- package/dist/messages.js.map +1 -0
- package/dist/package-metadata.d.ts +3 -0
- package/dist/package-metadata.d.ts.map +1 -0
- package/dist/package-metadata.js +5 -0
- package/dist/package-metadata.js.map +1 -0
- package/dist/sealed/sealer.d.ts +17 -0
- package/dist/sealed/sealer.d.ts.map +1 -0
- package/dist/sealed/sealer.js +73 -0
- package/dist/sealed/sealer.js.map +1 -0
- package/dist/signed/signer.d.ts +9 -0
- package/dist/signed/signer.d.ts.map +1 -0
- package/dist/signed/signer.js +31 -0
- package/dist/signed/signer.js.map +1 -0
- package/dist/src/basics.d.ts +7 -0
- package/dist/src/basics.d.ts.map +1 -0
- package/dist/src/basics.js +17 -0
- package/dist/src/basics.js.map +1 -0
- package/dist/src/cli.d.ts +3 -0
- package/dist/src/cli.d.ts.map +1 -0
- package/dist/src/cli.js +135 -0
- package/dist/src/cli.js.map +1 -0
- package/dist/src/errors.d.ts +9 -0
- package/dist/src/errors.d.ts.map +1 -0
- package/dist/src/errors.js +12 -0
- package/dist/src/errors.js.map +1 -0
- package/dist/src/files/files.d.ts +56 -0
- package/dist/src/files/files.d.ts.map +1 -0
- package/dist/src/files/files.js +117 -0
- package/dist/src/files/files.js.map +1 -0
- package/dist/src/files/format.d.ts +43 -0
- package/dist/src/files/format.d.ts.map +1 -0
- package/dist/src/files/format.js +82 -0
- package/dist/src/files/format.js.map +1 -0
- package/dist/src/files/slots.d.ts +20 -0
- package/dist/src/files/slots.d.ts.map +1 -0
- package/dist/src/files/slots.js +125 -0
- package/dist/src/files/slots.js.map +1 -0
- package/dist/src/files/streams.d.ts +18 -0
- package/dist/src/files/streams.d.ts.map +1 -0
- package/dist/src/files/streams.js +140 -0
- package/dist/src/files/streams.js.map +1 -0
- package/dist/src/index.d.ts +20 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +12 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/keys/env.d.ts +17 -0
- package/dist/src/keys/env.d.ts.map +1 -0
- package/dist/src/keys/env.js +99 -0
- package/dist/src/keys/env.js.map +1 -0
- package/dist/src/keys/ring.d.ts +13 -0
- package/dist/src/keys/ring.d.ts.map +1 -0
- package/dist/src/keys/ring.js +66 -0
- package/dist/src/keys/ring.js.map +1 -0
- package/dist/src/logging.d.ts +3 -0
- package/dist/src/logging.d.ts.map +1 -0
- package/dist/src/logging.js +7 -0
- package/dist/src/logging.js.map +1 -0
- package/dist/src/messages.d.ts +9 -0
- package/dist/src/messages.d.ts.map +1 -0
- package/dist/src/messages.js +29 -0
- package/dist/src/messages.js.map +1 -0
- package/dist/src/package-metadata.d.ts +3 -0
- package/dist/src/package-metadata.d.ts.map +1 -0
- package/dist/src/package-metadata.js +5 -0
- package/dist/src/package-metadata.js.map +1 -0
- package/dist/src/sealed/sealer.d.ts +17 -0
- package/dist/src/sealed/sealer.d.ts.map +1 -0
- package/dist/src/sealed/sealer.js +73 -0
- package/dist/src/sealed/sealer.js.map +1 -0
- package/dist/src/signed/signer.d.ts +9 -0
- package/dist/src/signed/signer.d.ts.map +1 -0
- package/dist/src/signed/signer.js +31 -0
- package/dist/src/signed/signer.js.map +1 -0
- package/package.json +82 -4
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import * as result from "@trebired/result";
|
|
2
|
+
import { type DecryptOptions, type EncryptOptions } from "./streams.js";
|
|
3
|
+
type FileInfo = {
|
|
4
|
+
created: string;
|
|
5
|
+
kind: string;
|
|
6
|
+
meta: Record<string, unknown>;
|
|
7
|
+
unlock: {
|
|
8
|
+
keyAvailable: boolean;
|
|
9
|
+
keyIds: string[];
|
|
10
|
+
password: boolean;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
type FilePaths = {
|
|
14
|
+
source: string;
|
|
15
|
+
target: string;
|
|
16
|
+
};
|
|
17
|
+
declare function encryptFile(options: EncryptOptions & FilePaths): Promise<result.ResultLike<{
|
|
18
|
+
code: import("../errors.js").CryptoErrorCode;
|
|
19
|
+
}, unknown, {
|
|
20
|
+
data: {
|
|
21
|
+
code: import("../errors.js").CryptoErrorCode;
|
|
22
|
+
};
|
|
23
|
+
}> | result.ResultLike<{
|
|
24
|
+
target: string;
|
|
25
|
+
}, unknown, {
|
|
26
|
+
message: false;
|
|
27
|
+
data: {
|
|
28
|
+
target: string;
|
|
29
|
+
};
|
|
30
|
+
}>>;
|
|
31
|
+
declare function decryptFile(options: DecryptOptions & FilePaths): Promise<result.ResultLike<{
|
|
32
|
+
code: import("../errors.js").CryptoErrorCode;
|
|
33
|
+
}, unknown, {
|
|
34
|
+
data: {
|
|
35
|
+
code: import("../errors.js").CryptoErrorCode;
|
|
36
|
+
};
|
|
37
|
+
}> | result.ResultLike<{
|
|
38
|
+
created: string;
|
|
39
|
+
kind: string;
|
|
40
|
+
meta: Record<string, unknown>;
|
|
41
|
+
target: string;
|
|
42
|
+
}, unknown, {
|
|
43
|
+
message: false;
|
|
44
|
+
data: {
|
|
45
|
+
created: string;
|
|
46
|
+
kind: string;
|
|
47
|
+
meta: Record<string, unknown>;
|
|
48
|
+
target: string;
|
|
49
|
+
};
|
|
50
|
+
}>>;
|
|
51
|
+
declare function inspectFile(filePath: string): FileInfo | null;
|
|
52
|
+
declare function isEncryptedFile(filePath: string): boolean;
|
|
53
|
+
declare function encryptedFileName(kind: string, date?: Date): string;
|
|
54
|
+
export { decryptFile, encryptedFileName, encryptFile, inspectFile, isEncryptedFile };
|
|
55
|
+
export type { FileInfo, FilePaths };
|
|
56
|
+
//# sourceMappingURL=files.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.d.ts","sourceRoot":"","sources":["../../../src/files/files.ts"],"names":[],"mappings":"AAEA,OAAO,KAAI,MAAM,MAAM,kBAAkB,CAAC;AAI1C,OAAO,EAA4C,KAAK,cAAc,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAElH,KAAK,QAAQ,GAAG;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9B,MAAM,EAAE;QAAE,YAAY,EAAE,OAAO,CAAC;QAAC,MAAM,EAAE,MAAM,EAAE,CAAC;QAAC,QAAQ,EAAE,OAAO,CAAA;KAAE,CAAC;CACxE,CAAC;AAEF,KAAK,SAAS,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAsBpD,iBAAe,WAAW,CAAC,OAAO,EAAE,cAAc,GAAC,SAAS;;;;;;;;;;;;;IAO3D;AAED,iBAAe,WAAW,CAAC,OAAO,EAAE,cAAc,GAAC,SAAS;;;;;;;;;;;;;;;;;;;IAmB3D;AAmBD,iBAAS,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,QAAQ,GAAG,IAAI,CAoBtD;AAED,iBAAS,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAYlD;AAED,iBAAS,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,OAAa,GAAG,MAAM,CAElE;AAED,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,WAAW,EAAE,WAAW,EAAE,eAAe,EAAE,CAAC;AACrF,YAAY,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { pipeline } from "node:stream/promises";
|
|
3
|
+
import * as result from "@trebired/result";
|
|
4
|
+
import { isCryptoError } from "../errors.js";
|
|
5
|
+
import { hasKeyRing, loadKeyRing } from "../keys/env.js";
|
|
6
|
+
import { decodePrefix, FILE_EXTENSION, hasMagic, parseHeader, PREFIX_BYTES } from "./format.js";
|
|
7
|
+
import { createDecryptStream, createEncryptStream } from "./streams.js";
|
|
8
|
+
function partialPath(target) {
|
|
9
|
+
return `${target}.partial`;
|
|
10
|
+
}
|
|
11
|
+
function failure(error) {
|
|
12
|
+
if (!isCryptoError(error))
|
|
13
|
+
throw error;
|
|
14
|
+
return result.badRequest(error.code, { data: { code: error.code } });
|
|
15
|
+
}
|
|
16
|
+
async function writeThrough(paths, transform) {
|
|
17
|
+
const partial = partialPath(paths.target);
|
|
18
|
+
try {
|
|
19
|
+
await pipeline(fs.createReadStream(paths.source), transform, fs.createWriteStream(partial, { mode: 0o600 }));
|
|
20
|
+
fs.renameSync(partial, paths.target);
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
fs.rmSync(partial, { force: true });
|
|
24
|
+
throw error;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async function encryptFile(options) {
|
|
28
|
+
try {
|
|
29
|
+
await writeThrough(options, await createEncryptStream(options));
|
|
30
|
+
return result.ok("crypto-file-encrypted", { message: false, data: { target: options.target } });
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
return failure(error);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async function decryptFile(options) {
|
|
37
|
+
let header = null;
|
|
38
|
+
const stream = createDecryptStream({
|
|
39
|
+
...options,
|
|
40
|
+
onHeader: (value) => {
|
|
41
|
+
header = value;
|
|
42
|
+
options.onHeader?.(value);
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
try {
|
|
46
|
+
await writeThrough(options, stream);
|
|
47
|
+
const opened = header;
|
|
48
|
+
return result.ok("crypto-file-decrypted", {
|
|
49
|
+
message: false,
|
|
50
|
+
data: { created: opened.created, kind: opened.kind, meta: opened.meta, target: options.target },
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
return failure(error);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function readHeaderBytes(filePath) {
|
|
58
|
+
const fd = fs.openSync(filePath, "r");
|
|
59
|
+
try {
|
|
60
|
+
const prefix = Buffer.alloc(PREFIX_BYTES);
|
|
61
|
+
if (fs.readSync(fd, prefix, 0, PREFIX_BYTES, 0) < PREFIX_BYTES || !hasMagic(prefix))
|
|
62
|
+
return null;
|
|
63
|
+
const json = Buffer.alloc(decodePrefix(prefix));
|
|
64
|
+
if (fs.readSync(fd, json, 0, json.length, PREFIX_BYTES) < json.length)
|
|
65
|
+
return null;
|
|
66
|
+
return json;
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
fs.closeSync(fd);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function availableKeyIds() {
|
|
73
|
+
return hasKeyRing() ? new Set(loadKeyRing().keys.keys()) : new Set();
|
|
74
|
+
}
|
|
75
|
+
function inspectFile(filePath) {
|
|
76
|
+
try {
|
|
77
|
+
const json = readHeaderBytes(filePath);
|
|
78
|
+
if (!json)
|
|
79
|
+
return null;
|
|
80
|
+
const header = parseHeader(json);
|
|
81
|
+
const keyIds = header.slots.flatMap((slot) => (slot.type === "key" ? [slot.keyId] : []));
|
|
82
|
+
const known = availableKeyIds();
|
|
83
|
+
return {
|
|
84
|
+
created: header.created,
|
|
85
|
+
kind: header.kind,
|
|
86
|
+
meta: header.meta,
|
|
87
|
+
unlock: {
|
|
88
|
+
keyAvailable: keyIds.some((id) => known.has(id)),
|
|
89
|
+
keyIds,
|
|
90
|
+
password: header.slots.some((slot) => slot.type === "password"),
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function isEncryptedFile(filePath) {
|
|
99
|
+
try {
|
|
100
|
+
const fd = fs.openSync(filePath, "r");
|
|
101
|
+
try {
|
|
102
|
+
const prefix = Buffer.alloc(PREFIX_BYTES);
|
|
103
|
+
return fs.readSync(fd, prefix, 0, PREFIX_BYTES, 0) === PREFIX_BYTES && hasMagic(prefix);
|
|
104
|
+
}
|
|
105
|
+
finally {
|
|
106
|
+
fs.closeSync(fd);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function encryptedFileName(kind, date = new Date()) {
|
|
114
|
+
return `${kind}-${date.toISOString().slice(0, 10)}${FILE_EXTENSION}`;
|
|
115
|
+
}
|
|
116
|
+
export { decryptFile, encryptedFileName, encryptFile, inspectFile, isEncryptedFile };
|
|
117
|
+
//# sourceMappingURL=files.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.js","sourceRoot":"","sources":["../../../src/files/files.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,KAAI,MAAM,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,QAAQ,EAAE,WAAW,EAAE,YAAY,EAAmB,MAAM,aAAa,CAAC;AACjH,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAA4C,MAAM,cAAc,CAAC;AAWlH,SAAS,WAAW,CAAC,MAAc;IACjC,OAAO,GAAG,MAAM,UAAU,CAAC;AAC7B,CAAC;AAED,SAAS,OAAO,CAAC,KAAc;IAC7B,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC;QAAE,MAAM,KAAK,CAAC;IACvC,OAAO,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;AACvE,CAAC;AAED,KAAK,UAAU,YAAY,CAAC,KAAgB,EAAE,SAAiC;IAC7E,MAAM,OAAO,GAAG,WAAW,CAAC,KAAK,CAAC,MAAM,CAAC,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C,EAAE,EAAE,MAAM,EAAE,CAAC,EAAE,YAAY,EAAE,CAAC,CAAC,KAAK,YAAY,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC;QAC1F,CAAC;gBAAS,CAAC;YACT,EAAE,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;QACnB,CAAC;IACH,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED,SAAS,iBAAiB,CAAC,IAAY,EAAE,IAAI,GAAG,IAAI,IAAI,EAAE;IACxD,OAAO,GAAG,IAAI,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,cAAc,EAAE,CAAC;AACvE,CAAC;AAED,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,WAAW,EAAE,WAAW,EAAE,eAAe,EAAE,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
type PasswordSlot = {
|
|
2
|
+
kdf: "argon2id";
|
|
3
|
+
memory: number;
|
|
4
|
+
parallelism: number;
|
|
5
|
+
passes: number;
|
|
6
|
+
salt: string;
|
|
7
|
+
type: "password";
|
|
8
|
+
wrapped: string;
|
|
9
|
+
};
|
|
10
|
+
type KeySlot = {
|
|
11
|
+
keyId: string;
|
|
12
|
+
type: "key";
|
|
13
|
+
wrapped: string;
|
|
14
|
+
};
|
|
15
|
+
type FileSlot = KeySlot | PasswordSlot;
|
|
16
|
+
type FileHeader = {
|
|
17
|
+
chunk: number;
|
|
18
|
+
cipher: "aes-256-gcm";
|
|
19
|
+
created: string;
|
|
20
|
+
kind: string;
|
|
21
|
+
meta: Record<string, unknown>;
|
|
22
|
+
nonce: string;
|
|
23
|
+
slots: FileSlot[];
|
|
24
|
+
};
|
|
25
|
+
declare const MAGIC: Buffer<ArrayBuffer>;
|
|
26
|
+
declare const PREFIX_BYTES: number;
|
|
27
|
+
declare const DEFAULT_CHUNK_BYTES: number;
|
|
28
|
+
declare const MIN_CHUNK_BYTES = 1024;
|
|
29
|
+
declare const MAX_CHUNK_BYTES: number;
|
|
30
|
+
declare const NONCE_PREFIX_BYTES = 7;
|
|
31
|
+
declare const TAG_BYTES = 16;
|
|
32
|
+
declare const KIND_PATTERN: RegExp;
|
|
33
|
+
declare const FILE_EXTENSION = ".tb.enc";
|
|
34
|
+
declare const MEDIA_TYPE = "application/vnd.trebired.encrypted";
|
|
35
|
+
declare function hasMagic(bytes: Buffer): boolean;
|
|
36
|
+
declare function encodeHeader(header: FileHeader): Buffer;
|
|
37
|
+
declare function decodePrefix(bytes: Buffer): number;
|
|
38
|
+
declare function parseHeader(bytes: Buffer): FileHeader;
|
|
39
|
+
declare function chunkNonce(prefix: Buffer, counter: number, last: boolean): Buffer;
|
|
40
|
+
declare function headerDigest(headerBytes: Buffer): Buffer;
|
|
41
|
+
export { chunkNonce, DEFAULT_CHUNK_BYTES, decodePrefix, encodeHeader, FILE_EXTENSION, hasMagic, headerDigest, KIND_PATTERN, MAGIC, MAX_CHUNK_BYTES, MEDIA_TYPE, MIN_CHUNK_BYTES, NONCE_PREFIX_BYTES, parseHeader, PREFIX_BYTES, TAG_BYTES, };
|
|
42
|
+
export type { FileHeader, FileSlot, KeySlot, PasswordSlot };
|
|
43
|
+
//# sourceMappingURL=format.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../../../src/files/format.ts"],"names":[],"mappings":"AAGA,KAAK,YAAY,GAAG;IAClB,GAAG,EAAE,UAAU,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,UAAU,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,KAAK,OAAO,GAAG;IACb,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,K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Z,YAAY,EACZ,cAAc,EACd,QAAQ,EACR,YAAY,EACZ,YAAY,EACZ,KAAK,EACL,eAAe,EACf,UAAU,EACV,eAAe,EACf,kBAAkB,EAClB,WAAW,EACX,YAAY,EACZ,SAAS,GACV,CAAC;AACF,YAAY,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,EAAE,YAAY,EAAE,CAAC"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { CryptoError } from "../errors.js";
|
|
3
|
+
const MAGIC = Buffer.from([0x54, 0x42, 0x45, 0x4e, 0x43, 0x0d, 0x0a, 0x1a]);
|
|
4
|
+
const FORMAT_VERSION = 1;
|
|
5
|
+
const PREFIX_BYTES = MAGIC.length + 5;
|
|
6
|
+
const MAX_HEADER_BYTES = 64 * 1024;
|
|
7
|
+
const DEFAULT_CHUNK_BYTES = 64 * 1024;
|
|
8
|
+
const MIN_CHUNK_BYTES = 1024;
|
|
9
|
+
const MAX_CHUNK_BYTES = 16 * 1024 * 1024;
|
|
10
|
+
const NONCE_PREFIX_BYTES = 7;
|
|
11
|
+
const TAG_BYTES = 16;
|
|
12
|
+
const MAX_CHUNKS = 2 ** 32 - 1;
|
|
13
|
+
const KIND_PATTERN = /^[a-z0-9][a-z0-9._-]{0,63}$/u;
|
|
14
|
+
const FILE_EXTENSION = ".tb.enc";
|
|
15
|
+
const MEDIA_TYPE = "application/vnd.trebired.encrypted";
|
|
16
|
+
function hasMagic(bytes) {
|
|
17
|
+
return bytes.length >= MAGIC.length && bytes.subarray(0, MAGIC.length).equals(MAGIC);
|
|
18
|
+
}
|
|
19
|
+
function encodeHeader(header) {
|
|
20
|
+
const json = Buffer.from(JSON.stringify(header), "utf8");
|
|
21
|
+
if (json.length > MAX_HEADER_BYTES)
|
|
22
|
+
throw new Error("crypto: the file header is larger than 64 KiB; shorten meta");
|
|
23
|
+
const prefix = Buffer.alloc(PREFIX_BYTES);
|
|
24
|
+
MAGIC.copy(prefix, 0);
|
|
25
|
+
prefix.writeUInt8(FORMAT_VERSION, MAGIC.length);
|
|
26
|
+
prefix.writeUInt32BE(json.length, MAGIC.length + 1);
|
|
27
|
+
return Buffer.concat([prefix, json]);
|
|
28
|
+
}
|
|
29
|
+
function decodePrefix(bytes) {
|
|
30
|
+
if (!hasMagic(bytes))
|
|
31
|
+
throw new CryptoError("crypto-file-not-encrypted");
|
|
32
|
+
const version = bytes.readUInt8(MAGIC.length);
|
|
33
|
+
if (version !== FORMAT_VERSION)
|
|
34
|
+
throw new CryptoError("crypto-file-unsupported", `format version ${version}`);
|
|
35
|
+
const length = bytes.readUInt32BE(MAGIC.length + 1);
|
|
36
|
+
if (length < 2 || length > MAX_HEADER_BYTES)
|
|
37
|
+
throw new CryptoError("crypto-file-damaged", "bad header length");
|
|
38
|
+
return length;
|
|
39
|
+
}
|
|
40
|
+
function readJson(bytes) {
|
|
41
|
+
try {
|
|
42
|
+
const parsed = JSON.parse(bytes.toString("utf8"));
|
|
43
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed))
|
|
44
|
+
return parsed;
|
|
45
|
+
}
|
|
46
|
+
catch { }
|
|
47
|
+
throw new CryptoError("crypto-file-damaged", "the header is not JSON");
|
|
48
|
+
}
|
|
49
|
+
function validChunk(value) {
|
|
50
|
+
return Number.isInteger(value) && value >= MIN_CHUNK_BYTES && value <= MAX_CHUNK_BYTES;
|
|
51
|
+
}
|
|
52
|
+
function parseHeader(bytes) {
|
|
53
|
+
const raw = readJson(bytes);
|
|
54
|
+
if (raw.cipher !== "aes-256-gcm")
|
|
55
|
+
throw new CryptoError("crypto-file-unsupported", `cipher ${String(raw.cipher)}`);
|
|
56
|
+
const nonce = Buffer.from(String(raw.nonce || ""), "base64url");
|
|
57
|
+
const valid = validChunk(raw.chunk) &&
|
|
58
|
+
nonce.length === NONCE_PREFIX_BYTES &&
|
|
59
|
+
typeof raw.kind === "string" &&
|
|
60
|
+
KIND_PATTERN.test(raw.kind) &&
|
|
61
|
+
typeof raw.created === "string" &&
|
|
62
|
+
Array.isArray(raw.slots) &&
|
|
63
|
+
raw.slots.length > 0;
|
|
64
|
+
if (!valid)
|
|
65
|
+
throw new CryptoError("crypto-file-damaged", "the header is incomplete");
|
|
66
|
+
const meta = raw.meta && typeof raw.meta === "object" && !Array.isArray(raw.meta) ? raw.meta : {};
|
|
67
|
+
return { ...raw, meta: meta };
|
|
68
|
+
}
|
|
69
|
+
function chunkNonce(prefix, counter, last) {
|
|
70
|
+
if (counter > MAX_CHUNKS)
|
|
71
|
+
throw new CryptoError("crypto-file-damaged", "too many chunks");
|
|
72
|
+
const nonce = Buffer.alloc(NONCE_PREFIX_BYTES + 5);
|
|
73
|
+
prefix.copy(nonce, 0);
|
|
74
|
+
nonce.writeUInt32BE(counter, NONCE_PREFIX_BYTES);
|
|
75
|
+
nonce.writeUInt8(last ? 1 : 0, NONCE_PREFIX_BYTES + 4);
|
|
76
|
+
return nonce;
|
|
77
|
+
}
|
|
78
|
+
function headerDigest(headerBytes) {
|
|
79
|
+
return createHash("sha256").update(headerBytes).digest();
|
|
80
|
+
}
|
|
81
|
+
export { chunkNonce, DEFAULT_CHUNK_BYTES, decodePrefix, encodeHeader, FILE_EXTENSION, hasMagic, headerDigest, KIND_PATTERN, MAGIC, MAX_CHUNK_BYTES, MEDIA_TYPE, MIN_CHUNK_BYTES, NONCE_PREFIX_BYTES, parseHeader, PREFIX_BYTES, TAG_BYTES, };
|
|
82
|
+
//# sourceMappingURL=format.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format.js","sourceRoot":"","sources":["../../../src/files/format.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AA8B5C,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAA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AAI,WAAW,CAAC,qBAAqB,EAAE,iBAAiB,CAAC,CAAC;IAC1F,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,kBAAkB,GAAG,CAAC,CAAC,CAAC;IACnD,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IACtB,KAAK,CAAC,aAAa,CAAC,OAAO,EAAE,kBAAkB,CAAC,CAAC;IACjD,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,kBAAkB,GAAG,CAAC,CAAC,CAAC;IACvD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,YAAY,CAAC,WAAmB;IACvC,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,MAAM,EAAE,CAAC;AAC3D,CAAC;AAED,OAAO,EACL,UAAU,EACV,mBAAmB,EACnB,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,QAAQ,EACR,YAAY,EACZ,YAAY,EACZ,KAAK,EACL,eAAe,EACf,UAAU,EACV,eAAe,EACf,kBAAkB,EAClB,WAAW,EACX,YAAY,EACZ,SAAS,GACV,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { FileHeader, FileSlot } from "./format.js";
|
|
2
|
+
type PasswordCost = {
|
|
3
|
+
memory: number;
|
|
4
|
+
parallelism: number;
|
|
5
|
+
passes: number;
|
|
6
|
+
};
|
|
7
|
+
type SlotOptions = {
|
|
8
|
+
keyRing?: boolean;
|
|
9
|
+
password?: string;
|
|
10
|
+
passwordCost?: Partial<PasswordCost>;
|
|
11
|
+
};
|
|
12
|
+
declare const MIN_PASSWORD_LENGTH = 8;
|
|
13
|
+
declare function createSlots(options: SlotOptions): Promise<{
|
|
14
|
+
fileKey: Buffer;
|
|
15
|
+
slots: FileSlot[];
|
|
16
|
+
}>;
|
|
17
|
+
declare function unlockFileKey(header: FileHeader, password?: string): Promise<Buffer>;
|
|
18
|
+
export { createSlots, MIN_PASSWORD_LENGTH, unlockFileKey };
|
|
19
|
+
export type { PasswordCost, SlotOptions };
|
|
20
|
+
//# sourceMappingURL=slots.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"slots.d.ts","sourceRoot":"","sources":["../../../src/files/slots.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAyB,MAAM,aAAa,CAAC;AAE/E,KAAK,YAAY,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAE5E,KAAK,WAAW,GAAG;IACjB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC;CACtC,CAAC;AAMF,QAAA,MAAM,mBAAmB,IAAI,CAAC;AAgE9B,iBAAe,WAAW,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC;IAAC,OAAO,EAAC,MAAM,CAAC;IAAA,KAAK,EAAC,QAAQ,EAAE,CAAA;CAAC,CAAC,CAS3F;AAyBD,iBAAe,aAAa,CAAC,MAAM,EAAE,UAAU,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAYnF;AAED,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,aAAa,EAAE,CAAC;AAC3D,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { argon2, createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
|
|
2
|
+
import { CryptoError } from "../errors.js";
|
|
3
|
+
import { hasKeyRing, loadKeyRing } from "../keys/env.js";
|
|
4
|
+
import { subkey } from "../keys/ring.js";
|
|
5
|
+
const FILE_KEY_BYTES = 32;
|
|
6
|
+
const WRAP_IV_BYTES = 12;
|
|
7
|
+
const WRAP_TAG_BYTES = 16;
|
|
8
|
+
const SALT_BYTES = 16;
|
|
9
|
+
const MIN_PASSWORD_LENGTH = 8;
|
|
10
|
+
const FILES_PURPOSE = "files";
|
|
11
|
+
const DEFAULT_PASSWORD_COST = { memory: 64 * 1024, parallelism: 4, passes: 3 };
|
|
12
|
+
const COST_LIMITS = {
|
|
13
|
+
memory: [8 * 1024, 1024 * 1024],
|
|
14
|
+
parallelism: [1, 16],
|
|
15
|
+
passes: [1, 16],
|
|
16
|
+
};
|
|
17
|
+
function withinLimits(cost) {
|
|
18
|
+
return Object.keys(COST_LIMITS).every((name) => {
|
|
19
|
+
const [min, max] = COST_LIMITS[name];
|
|
20
|
+
return Number.isInteger(cost[name]) && cost[name] >= min && cost[name] <= max;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
function passwordKey(password, salt, cost) {
|
|
24
|
+
const parameters = { ...cost, message: password, nonce: salt, tagLength: FILE_KEY_BYTES };
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
argon2("argon2id", parameters, (error, key) => (error ? reject(error) : resolve(Buffer.from(key))));
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function slotData(index) {
|
|
30
|
+
return Buffer.from(`tbenc1|slot|${index}`, "utf8");
|
|
31
|
+
}
|
|
32
|
+
function wrapKey(kek, fileKey, index) {
|
|
33
|
+
const iv = randomBytes(WRAP_IV_BYTES);
|
|
34
|
+
const cipher = createCipheriv("aes-256-gcm", kek, iv);
|
|
35
|
+
cipher.setAAD(slotData(index));
|
|
36
|
+
return Buffer.concat([iv, cipher.update(fileKey), cipher.final(), cipher.getAuthTag()]).toString("base64url");
|
|
37
|
+
}
|
|
38
|
+
function unwrapKey(kek, wrapped, index) {
|
|
39
|
+
const bytes = Buffer.from(String(wrapped || ""), "base64url");
|
|
40
|
+
if (bytes.length !== WRAP_IV_BYTES + FILE_KEY_BYTES + WRAP_TAG_BYTES)
|
|
41
|
+
return null;
|
|
42
|
+
const decipher = createDecipheriv("aes-256-gcm", kek, bytes.subarray(0, WRAP_IV_BYTES));
|
|
43
|
+
decipher.setAAD(slotData(index));
|
|
44
|
+
decipher.setAuthTag(bytes.subarray(WRAP_IV_BYTES + FILE_KEY_BYTES));
|
|
45
|
+
try {
|
|
46
|
+
return Buffer.concat([decipher.update(bytes.subarray(WRAP_IV_BYTES, WRAP_IV_BYTES + FILE_KEY_BYTES)), decipher.final()]);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async function passwordSlot(password, fileKey, index, costInput) {
|
|
53
|
+
if (password.length < MIN_PASSWORD_LENGTH)
|
|
54
|
+
throw new CryptoError("crypto-password-too-short");
|
|
55
|
+
const cost = { ...DEFAULT_PASSWORD_COST, ...costInput };
|
|
56
|
+
if (!withinLimits(cost))
|
|
57
|
+
throw new Error("crypto: passwordCost is outside the supported limits");
|
|
58
|
+
const salt = randomBytes(SALT_BYTES);
|
|
59
|
+
const kek = await passwordKey(password, salt, cost);
|
|
60
|
+
const slot = { kdf: "argon2id", ...cost, salt: salt.toString("base64url"), type: "password", wrapped: "" };
|
|
61
|
+
slot.wrapped = wrapKey(kek, fileKey, index);
|
|
62
|
+
return slot;
|
|
63
|
+
}
|
|
64
|
+
function keySlot(fileKey, index) {
|
|
65
|
+
const ring = loadKeyRing();
|
|
66
|
+
const kek = subkey(ring.keys.get(ring.activeId), FILES_PURPOSE);
|
|
67
|
+
return { keyId: ring.activeId, type: "key", wrapped: wrapKey(kek, fileKey, index) };
|
|
68
|
+
}
|
|
69
|
+
async function createSlots(options) {
|
|
70
|
+
const fileKey = randomBytes(FILE_KEY_BYTES);
|
|
71
|
+
const slots = [];
|
|
72
|
+
if (options.keyRing)
|
|
73
|
+
slots.push(keySlot(fileKey, slots.length));
|
|
74
|
+
if (typeof options.password === "string") {
|
|
75
|
+
slots.push(await passwordSlot(options.password, fileKey, slots.length, options.passwordCost));
|
|
76
|
+
}
|
|
77
|
+
if (!slots.length)
|
|
78
|
+
throw new Error("crypto: give a password, keyRing: true, or both");
|
|
79
|
+
return { fileKey, slots };
|
|
80
|
+
}
|
|
81
|
+
function unlockWithKeyRing(slots) {
|
|
82
|
+
if (!hasKeyRing())
|
|
83
|
+
return null;
|
|
84
|
+
const ring = loadKeyRing();
|
|
85
|
+
for (const [index, slot] of slots.entries()) {
|
|
86
|
+
const master = slot.type === "key" ? ring.keys.get(slot.keyId) : undefined;
|
|
87
|
+
const fileKey = master ? unwrapKey(subkey(master, FILES_PURPOSE), slot.wrapped, index) : null;
|
|
88
|
+
if (fileKey)
|
|
89
|
+
return fileKey;
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
async function unlockWithPassword(slots, password) {
|
|
94
|
+
for (const [index, slot] of slots.entries()) {
|
|
95
|
+
if (slot.type !== "password")
|
|
96
|
+
continue;
|
|
97
|
+
const cost = { memory: slot.memory, parallelism: slot.parallelism, passes: slot.passes };
|
|
98
|
+
if (slot.kdf !== "argon2id" || !withinLimits(cost))
|
|
99
|
+
throw new CryptoError("crypto-file-unsupported", "password slot");
|
|
100
|
+
const kek = await passwordKey(password, Buffer.from(String(slot.salt || ""), "base64url"), cost);
|
|
101
|
+
const fileKey = unwrapKey(kek, slot.wrapped, index);
|
|
102
|
+
if (fileKey)
|
|
103
|
+
return fileKey;
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
async function unlockFileKey(header, password) {
|
|
108
|
+
const fromRing = unlockWithKeyRing(header.slots);
|
|
109
|
+
if (fromRing)
|
|
110
|
+
return fromRing;
|
|
111
|
+
const hasPasswordSlot = header.slots.some((slot) => slot.type === "password");
|
|
112
|
+
if (hasPasswordSlot && password) {
|
|
113
|
+
const fromPassword = await unlockWithPassword(header.slots, password);
|
|
114
|
+
if (fromPassword)
|
|
115
|
+
return fromPassword;
|
|
116
|
+
throw new CryptoError("crypto-password-wrong");
|
|
117
|
+
}
|
|
118
|
+
if (hasPasswordSlot)
|
|
119
|
+
throw new CryptoError("crypto-password-required");
|
|
120
|
+
if (header.slots.some((slot) => slot.type === "key"))
|
|
121
|
+
throw new CryptoError("crypto-key-missing");
|
|
122
|
+
throw new CryptoError("crypto-file-unsupported", "no known slot type");
|
|
123
|
+
}
|
|
124
|
+
export { createSlots, MIN_PASSWORD_LENGTH, unlockFileKey };
|
|
125
|
+
//# sourceMappingURL=slots.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"slots.js","sourceRoot":"","sources":["../../../src/files/slots.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACpF,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAWvC,MAAM,cAAc,GAAG,EAAE,CAAC;AAC1B,MAAM,aAAa,GAAG,EAAE,CAAC;AACzB,MAAM,cAAc,GAAG,EAAE,CAAC;AAC1B,MAAM,UAAU,GAAG,EAAE,CAAC;AACtB,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAC9B,MAAM,aAAa,GAAG,OAAO,CAAC;AAC9B,MAAM,qBAAqB,GAAiB,EAAE,MAAM,EAAE,EAAE,GAAG,IAAI,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC;AAC7F,MAAM,WAAW,GAAG;IAClB,MAAM,EAAE,CAAC,CAAC,GAAG,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC;IAC/B,WAAW,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC;IACpB,MAAM,EAAE,CAAC,CAAC,EAAE,EAAE,CAA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0B,CAAC,CAAC;IACvE,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,KAAK,CAAC;QAAE,MAAM,IAAI,WAAW,CAAC,oBAAoB,CAAC,CAAC;IAClG,MAAM,IAAI,WAAW,CAAC,yBAAyB,EAAE,oBAAoB,CAAC,CAAC;AACzE,CAAC;AAED,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,aAAa,EAAE,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Transform } from "node:stream";
|
|
2
|
+
import { type FileHeader } from "./format.js";
|
|
3
|
+
import { type SlotOptions } from "./slots.js";
|
|
4
|
+
type EncryptOptions = SlotOptions & {
|
|
5
|
+
chunkBytes?: number;
|
|
6
|
+
kind: string;
|
|
7
|
+
meta?: Record<string, unknown>;
|
|
8
|
+
};
|
|
9
|
+
type DecryptOptions = {
|
|
10
|
+
kind?: string;
|
|
11
|
+
onHeader?: (header: FileHeader) => void;
|
|
12
|
+
password?: string;
|
|
13
|
+
};
|
|
14
|
+
declare function createEncryptStream(options: EncryptOptions): Promise<Transform>;
|
|
15
|
+
declare function createDecryptStream(options?: DecryptOptions): Transform;
|
|
16
|
+
export { createDecryptStream, createEncryptStream };
|
|
17
|
+
export type { DecryptOptions, EncryptOptions };
|
|
18
|
+
//# sourceMappingURL=streams.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streams.d.ts","sourceRoot":"","sources":["../../../src/files/streams.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAA0B,MAAM,aAAa,CAAC;AAEhE,OAAO,EAaL,KAAK,UAAU,EAChB,MAAM,aAAa,CAAC;AACrB,OAAO,EAA8B,KAAK,WAAW,EAAE,MAAM,YAAY,CAAC;AAE1E,KAAK,cAAc,GAAG,WAAW,GAAE;IACjC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC,CAAC;AAEF,KAAK,cAAc,GAAG;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,UAAU,KAAK,IAAI,CAAC;IACxC,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAsHF,iBAAe,mBAAmB,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,SAAS,CAAC,CAgB9E;AAED,iBAAS,mBAAmB,CAAC,OAAO,GAAE,cAAmB,GAAG,SAAS,CAEpE;AAED,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,CAAC;AACpD,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,CAAC"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
|
|
2
|
+
import { Transform } from "node:stream";
|
|
3
|
+
import { CryptoError } from "../errors.js";
|
|
4
|
+
import { chunkNonce, DEFAULT_CHUNK_BYTES, decodePrefix, encodeHeader, headerDigest, KIND_PATTERN, MAX_CHUNK_BYTES, MIN_CHUNK_BYTES, NONCE_PREFIX_BYTES, parseHeader, PREFIX_BYTES, TAG_BYTES, } from "./format.js";
|
|
5
|
+
import { createSlots, unlockFileKey } from "./slots.js";
|
|
6
|
+
function sealChunk(keys, counter, last, plain) {
|
|
7
|
+
const cipher = createCipheriv("aes-256-gcm", keys.fileKey, chunkNonce(keys.prefix, counter, last));
|
|
8
|
+
cipher.setAAD(keys.aad);
|
|
9
|
+
return Buffer.concat([cipher.update(plain), cipher.final(), cipher.getAuthTag()]);
|
|
10
|
+
}
|
|
11
|
+
function openChunk(keys, counter, last, sealed) {
|
|
12
|
+
if (sealed.length < TAG_BYTES)
|
|
13
|
+
throw new CryptoError("crypto-file-damaged", "the file is truncated");
|
|
14
|
+
const decipher = createDecipheriv("aes-256-gcm", keys.fileKey, chunkNonce(keys.prefix, counter, last));
|
|
15
|
+
decipher.setAAD(keys.aad);
|
|
16
|
+
decipher.setAuthTag(sealed.subarray(sealed.length - TAG_BYTES));
|
|
17
|
+
try {
|
|
18
|
+
return Buffer.concat([decipher.update(sealed.subarray(0, sealed.length - TAG_BYTES)), decipher.final()]);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
throw new CryptoError("crypto-file-damaged", `chunk ${counter} failed its check`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
class ChunkEncryptor extends Transform {
|
|
25
|
+
constructor(keys, headerBytes, chunkBytes) {
|
|
26
|
+
super();
|
|
27
|
+
this.keys = keys;
|
|
28
|
+
this.headerBytes = headerBytes;
|
|
29
|
+
this.chunkBytes = chunkBytes;
|
|
30
|
+
this.buffered = Buffer.alloc(0);
|
|
31
|
+
this.counter = 0;
|
|
32
|
+
this.headerSent = false;
|
|
33
|
+
}
|
|
34
|
+
sendHeader() {
|
|
35
|
+
if (this.headerSent)
|
|
36
|
+
return;
|
|
37
|
+
this.headerSent = true;
|
|
38
|
+
this.push(this.headerBytes);
|
|
39
|
+
}
|
|
40
|
+
_transform(chunk, _encoding, callback) {
|
|
41
|
+
this.sendHeader();
|
|
42
|
+
this.buffered = Buffer.concat([this.buffered, chunk]);
|
|
43
|
+
while (this.buffered.length > this.chunkBytes) {
|
|
44
|
+
this.push(sealChunk(this.keys, this.counter, false, this.buffered.subarray(0, this.chunkBytes)));
|
|
45
|
+
this.buffered = this.buffered.subarray(this.chunkBytes);
|
|
46
|
+
this.counter += 1;
|
|
47
|
+
}
|
|
48
|
+
callback();
|
|
49
|
+
}
|
|
50
|
+
_flush(callback) {
|
|
51
|
+
this.sendHeader();
|
|
52
|
+
this.push(sealChunk(this.keys, this.counter, true, this.buffered));
|
|
53
|
+
callback();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
class ChunkDecryptor extends Transform {
|
|
57
|
+
constructor(options) {
|
|
58
|
+
super();
|
|
59
|
+
this.options = options;
|
|
60
|
+
this.buffered = Buffer.alloc(0);
|
|
61
|
+
this.counter = 0;
|
|
62
|
+
this.keys = null;
|
|
63
|
+
this.sealedChunkBytes = 0;
|
|
64
|
+
}
|
|
65
|
+
async readHeader() {
|
|
66
|
+
if (this.buffered.length < PREFIX_BYTES)
|
|
67
|
+
return false;
|
|
68
|
+
const headerLength = decodePrefix(this.buffered.subarray(0, PREFIX_BYTES));
|
|
69
|
+
if (this.buffered.length < PREFIX_BYTES + headerLength)
|
|
70
|
+
return false;
|
|
71
|
+
const headerBytes = this.buffered.subarray(0, PREFIX_BYTES + headerLength);
|
|
72
|
+
const header = parseHeader(headerBytes.subarray(PREFIX_BYTES));
|
|
73
|
+
if (this.options.kind && header.kind !== this.options.kind)
|
|
74
|
+
throw new CryptoError("crypto-kind-mismatch", header.kind);
|
|
75
|
+
this.options.onHeader?.(header);
|
|
76
|
+
const fileKey = await unlockFileKey(header, this.options.password);
|
|
77
|
+
this.keys = { aad: headerDigest(headerBytes), fileKey, prefix: Buffer.from(header.nonce, "base64url") };
|
|
78
|
+
this.sealedChunkBytes = header.chunk + TAG_BYTES;
|
|
79
|
+
this.buffered = this.buffered.subarray(headerBytes.length);
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
async drain() {
|
|
83
|
+
if (!this.keys && !(await this.readHeader()))
|
|
84
|
+
return;
|
|
85
|
+
const keys = this.keys;
|
|
86
|
+
while (this.buffered.length > this.sealedChunkBytes) {
|
|
87
|
+
this.push(openChunk(keys, this.counter, false, this.buffered.subarray(0, this.sealedChunkBytes)));
|
|
88
|
+
this.buffered = this.buffered.subarray(this.sealedChunkBytes);
|
|
89
|
+
this.counter += 1;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
_transform(chunk, _encoding, callback) {
|
|
93
|
+
this.buffered = Buffer.concat([this.buffered, chunk]);
|
|
94
|
+
this.drain().then(() => callback(), callback);
|
|
95
|
+
}
|
|
96
|
+
_flush(callback) {
|
|
97
|
+
if (!this.keys) {
|
|
98
|
+
const code = this.buffered.length < PREFIX_BYTES ? "crypto-file-not-encrypted" : "crypto-file-damaged";
|
|
99
|
+
callback(new CryptoError(code, "the header is incomplete"));
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
try {
|
|
103
|
+
this.push(openChunk(this.keys, this.counter, true, this.buffered));
|
|
104
|
+
callback();
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
callback(error);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function checkedChunkBytes(value) {
|
|
112
|
+
const chunk = value ?? DEFAULT_CHUNK_BYTES;
|
|
113
|
+
if (!Number.isInteger(chunk) || chunk < MIN_CHUNK_BYTES || chunk > MAX_CHUNK_BYTES) {
|
|
114
|
+
throw new Error("crypto: chunkBytes must be an integer from 1 KiB to 16 MiB");
|
|
115
|
+
}
|
|
116
|
+
return chunk;
|
|
117
|
+
}
|
|
118
|
+
async function createEncryptStream(options) {
|
|
119
|
+
if (!KIND_PATTERN.test(options.kind))
|
|
120
|
+
throw new Error(`crypto: invalid file kind "${options.kind}"`);
|
|
121
|
+
const chunk = checkedChunkBytes(options.chunkBytes);
|
|
122
|
+
const { fileKey, slots } = await createSlots(options);
|
|
123
|
+
const prefix = randomBytes(NONCE_PREFIX_BYTES);
|
|
124
|
+
const header = {
|
|
125
|
+
chunk,
|
|
126
|
+
cipher: "aes-256-gcm",
|
|
127
|
+
created: new Date().toISOString(),
|
|
128
|
+
kind: options.kind,
|
|
129
|
+
meta: options.meta || {},
|
|
130
|
+
nonce: prefix.toString("base64url"),
|
|
131
|
+
slots,
|
|
132
|
+
};
|
|
133
|
+
const headerBytes = encodeHeader(header);
|
|
134
|
+
return new ChunkEncryptor({ aad: headerDigest(headerBytes), fileKey, prefix }, headerBytes, chunk);
|
|
135
|
+
}
|
|
136
|
+
function createDecryptStream(options = {}) {
|
|
137
|
+
return new ChunkDecryptor(options);
|
|
138
|
+
}
|
|
139
|
+
export { createDecryptStream, createEncryptStream };
|
|
140
|
+
//# sourceMappingURL=streams.js.map
|