@shieldfive/crypto 1.0.0-alpha.11
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/CHANGELOG.md +228 -0
- package/LICENSE +201 -0
- package/README.md +275 -0
- package/SECURITY.md +130 -0
- package/dist/format/header.d.ts +73 -0
- package/dist/format/header.d.ts.map +1 -0
- package/dist/format/header.js +230 -0
- package/dist/format/header.js.map +1 -0
- package/dist/identity/index.d.ts +119 -0
- package/dist/identity/index.d.ts.map +1 -0
- package/dist/identity/index.js +217 -0
- package/dist/identity/index.js.map +1 -0
- package/dist/identity/sign.d.ts +92 -0
- package/dist/identity/sign.d.ts.map +1 -0
- package/dist/identity/sign.js +182 -0
- package/dist/identity/sign.js.map +1 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +146 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/encoding.d.ts +33 -0
- package/dist/internal/encoding.d.ts.map +1 -0
- package/dist/internal/encoding.js +135 -0
- package/dist/internal/encoding.js.map +1 -0
- package/dist/internal/hkdf.d.ts +14 -0
- package/dist/internal/hkdf.d.ts.map +1 -0
- package/dist/internal/hkdf.js +27 -0
- package/dist/internal/hkdf.js.map +1 -0
- package/dist/internal/hmac.d.ts +9 -0
- package/dist/internal/hmac.d.ts.map +1 -0
- package/dist/internal/hmac.js +18 -0
- package/dist/internal/hmac.js.map +1 -0
- package/dist/internal/runtime.d.ts +29 -0
- package/dist/internal/runtime.d.ts.map +1 -0
- package/dist/internal/runtime.js +67 -0
- package/dist/internal/runtime.js.map +1 -0
- package/dist/internal/sodium.d.ts +12 -0
- package/dist/internal/sodium.d.ts.map +1 -0
- package/dist/internal/sodium.js +34 -0
- package/dist/internal/sodium.js.map +1 -0
- package/dist/internal/types.d.ts +98 -0
- package/dist/internal/types.d.ts.map +1 -0
- package/dist/internal/types.js +70 -0
- package/dist/internal/types.js.map +1 -0
- package/dist/kdf/argon2id.d.ts +72 -0
- package/dist/kdf/argon2id.d.ts.map +1 -0
- package/dist/kdf/argon2id.js +98 -0
- package/dist/kdf/argon2id.js.map +1 -0
- package/dist/migration/v0-bridge.d.ts +94 -0
- package/dist/migration/v0-bridge.d.ts.map +1 -0
- package/dist/migration/v0-bridge.js +203 -0
- package/dist/migration/v0-bridge.js.map +1 -0
- package/dist/streams/aes-gcm-v1.d.ts +144 -0
- package/dist/streams/aes-gcm-v1.d.ts.map +1 -0
- package/dist/streams/aes-gcm-v1.js +456 -0
- package/dist/streams/aes-gcm-v1.js.map +1 -0
- package/dist/streams/pq-hybrid-v1.d.ts +219 -0
- package/dist/streams/pq-hybrid-v1.d.ts.map +1 -0
- package/dist/streams/pq-hybrid-v1.js +473 -0
- package/dist/streams/pq-hybrid-v1.js.map +1 -0
- package/dist/suites/aes-gcm-v0/api.d.ts +48 -0
- package/dist/suites/aes-gcm-v0/api.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v0/api.js +110 -0
- package/dist/suites/aes-gcm-v0/api.js.map +1 -0
- package/dist/suites/aes-gcm-v1/api.d.ts +54 -0
- package/dist/suites/aes-gcm-v1/api.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v1/api.js +181 -0
- package/dist/suites/aes-gcm-v1/api.js.map +1 -0
- package/dist/suites/aes-gcm-v1/index.d.ts +59 -0
- package/dist/suites/aes-gcm-v1/index.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v1/index.js +177 -0
- package/dist/suites/aes-gcm-v1/index.js.map +1 -0
- package/dist/suites/aes-gcm-v2/api.d.ts +44 -0
- package/dist/suites/aes-gcm-v2/api.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v2/api.js +162 -0
- package/dist/suites/aes-gcm-v2/api.js.map +1 -0
- package/dist/suites/aes-gcm-v2/index.d.ts +63 -0
- package/dist/suites/aes-gcm-v2/index.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v2/index.js +193 -0
- package/dist/suites/aes-gcm-v2/index.js.map +1 -0
- package/dist/suites/pq-hybrid-v1/api.d.ts +67 -0
- package/dist/suites/pq-hybrid-v1/api.d.ts.map +1 -0
- package/dist/suites/pq-hybrid-v1/api.js +190 -0
- package/dist/suites/pq-hybrid-v1/api.js.map +1 -0
- package/dist/suites/pq-hybrid-v1/index.d.ts +101 -0
- package/dist/suites/pq-hybrid-v1/index.d.ts.map +1 -0
- package/dist/suites/pq-hybrid-v1/index.js +225 -0
- package/dist/suites/pq-hybrid-v1/index.js.map +1 -0
- package/dist/suites/xchacha-v1/api.d.ts +29 -0
- package/dist/suites/xchacha-v1/api.d.ts.map +1 -0
- package/dist/suites/xchacha-v1/api.js +154 -0
- package/dist/suites/xchacha-v1/api.js.map +1 -0
- package/dist/suites/xchacha-v1/index.d.ts +50 -0
- package/dist/suites/xchacha-v1/index.d.ts.map +1 -0
- package/dist/suites/xchacha-v1/index.js +133 -0
- package/dist/suites/xchacha-v1/index.js.map +1 -0
- package/package.json +124 -0
- package/spec/format-v0.md +81 -0
- package/spec/format-v1.md +400 -0
- package/spec/key-derivation.md +167 -0
- package/spec/threat-model.md +242 -0
- package/src/format/header.ts +316 -0
- package/src/identity/index.ts +371 -0
- package/src/identity/sign.ts +221 -0
- package/src/index.ts +224 -0
- package/src/internal/encoding.ts +152 -0
- package/src/internal/hkdf.ts +47 -0
- package/src/internal/hmac.ts +33 -0
- package/src/internal/runtime.ts +80 -0
- package/src/internal/sodium.ts +38 -0
- package/src/internal/types.ts +134 -0
- package/src/kdf/argon2id.ts +165 -0
- package/src/migration/v0-bridge.ts +347 -0
- package/src/streams/aes-gcm-v1.ts +647 -0
- package/src/streams/pq-hybrid-v1.ts +722 -0
- package/src/suites/aes-gcm-v0/api.ts +155 -0
- package/src/suites/aes-gcm-v1/api.ts +292 -0
- package/src/suites/aes-gcm-v1/index.ts +269 -0
- package/src/suites/aes-gcm-v2/api.ts +268 -0
- package/src/suites/aes-gcm-v2/index.ts +289 -0
- package/src/suites/pq-hybrid-v1/api.ts +299 -0
- package/src/suites/pq-hybrid-v1/index.ts +343 -0
- package/src/suites/xchacha-v1/api.ts +254 -0
- package/src/suites/xchacha-v1/index.ts +213 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* High-level encrypt/decrypt API for the XChaCha20-Poly1305 v1 suite.
|
|
3
|
+
* Mirrors the shape of the AES-GCM v1 API; behavior is identical at the
|
|
4
|
+
* format layer.
|
|
5
|
+
*/
|
|
6
|
+
import { buildAuthenticatedHeader, parseHeader, verifyHeaderMac, HeaderError, } from '../../format/header.js';
|
|
7
|
+
import { XCHACHA_V1_SUITE_PAYLOAD_LENGTH, XCHACHA_V1_TAG_BYTES, createChunkContext, decryptChunk, encryptChunk, generateContentMaterial, parseXChaChaV1SuitePayload, } from './index.js';
|
|
8
|
+
import { DEFAULT_CHUNK_SIZE, HEADER_SIZES, MAX_TOTAL_CHUNKS, SUITE, } from '../../internal/types.js';
|
|
9
|
+
import { asBlobPart, readUint32BE, uint32BE, } from '../../internal/encoding.js';
|
|
10
|
+
const LENGTH_PREFIX_BYTES = 4;
|
|
11
|
+
export async function encryptBlob(options) {
|
|
12
|
+
const { blob, onProgress } = options;
|
|
13
|
+
if (!(blob instanceof Blob)) {
|
|
14
|
+
throw new TypeError('encryptBlob: blob must be a Blob or File');
|
|
15
|
+
}
|
|
16
|
+
const chunkSize = options.chunkSize ?? DEFAULT_CHUNK_SIZE;
|
|
17
|
+
if (!Number.isSafeInteger(chunkSize) || chunkSize <= 0) {
|
|
18
|
+
throw new RangeError('encryptBlob: chunkSize must be a positive integer');
|
|
19
|
+
}
|
|
20
|
+
const plaintextSize = blob.size;
|
|
21
|
+
const totalChunks = plaintextSize === 0 ? 0 : Math.ceil(plaintextSize / chunkSize);
|
|
22
|
+
if (totalChunks > MAX_TOTAL_CHUNKS) {
|
|
23
|
+
throw new RangeError('encryptBlob: file would exceed total_chunks limit');
|
|
24
|
+
}
|
|
25
|
+
const generated = generateContentMaterial();
|
|
26
|
+
const contentKey = options.contentKey ?? generated.contentKey;
|
|
27
|
+
const fileId = options.fileId ?? generated.fileId;
|
|
28
|
+
if (contentKey.length !== 32) {
|
|
29
|
+
throw new RangeError('encryptBlob: contentKey must be 32 bytes');
|
|
30
|
+
}
|
|
31
|
+
if (fileId.length !== HEADER_SIZES.FILE_ID) {
|
|
32
|
+
throw new RangeError('encryptBlob: fileId must be 16 bytes');
|
|
33
|
+
}
|
|
34
|
+
const suitePayload = options.suitePayloadOverride ??
|
|
35
|
+
new Uint8Array(XCHACHA_V1_SUITE_PAYLOAD_LENGTH);
|
|
36
|
+
if (suitePayload.length !== XCHACHA_V1_SUITE_PAYLOAD_LENGTH) {
|
|
37
|
+
throw new RangeError(`encryptBlob: suitePayloadOverride must be ${XCHACHA_V1_SUITE_PAYLOAD_LENGTH} bytes`);
|
|
38
|
+
}
|
|
39
|
+
const header = await buildAuthenticatedHeader({
|
|
40
|
+
suite: SUITE.XCHACHA20_POLY1305_V1,
|
|
41
|
+
fileId,
|
|
42
|
+
chunkSize,
|
|
43
|
+
totalChunks,
|
|
44
|
+
plaintextSize,
|
|
45
|
+
suitePayload,
|
|
46
|
+
}, contentKey);
|
|
47
|
+
const ctx = await createChunkContext(contentKey, fileId, totalChunks);
|
|
48
|
+
const parts = [asBlobPart(header)];
|
|
49
|
+
for (let i = 0; i < totalChunks; i += 1) {
|
|
50
|
+
const start = i * chunkSize;
|
|
51
|
+
const end = Math.min(start + chunkSize, plaintextSize);
|
|
52
|
+
const plaintextBytes = new Uint8Array(await blob.slice(start, end).arrayBuffer());
|
|
53
|
+
const ciphertext = await encryptChunk(ctx, i, plaintextBytes);
|
|
54
|
+
parts.push(asBlobPart(uint32BE(ciphertext.length)));
|
|
55
|
+
parts.push(asBlobPart(ciphertext));
|
|
56
|
+
onProgress?.((i + 1) / Math.max(totalChunks, 1));
|
|
57
|
+
}
|
|
58
|
+
if (totalChunks === 0)
|
|
59
|
+
onProgress?.(1);
|
|
60
|
+
return {
|
|
61
|
+
blob: new Blob(parts, { type: 'application/octet-stream' }),
|
|
62
|
+
suite: SUITE.XCHACHA20_POLY1305_V1,
|
|
63
|
+
fileId,
|
|
64
|
+
chunkSize,
|
|
65
|
+
totalChunks,
|
|
66
|
+
plaintextSize,
|
|
67
|
+
contentKey,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
export async function decryptBlob(options) {
|
|
71
|
+
const { blob, contentKey, onProgress } = options;
|
|
72
|
+
if (!(blob instanceof Blob)) {
|
|
73
|
+
throw new TypeError('decryptBlob: blob must be a Blob or File');
|
|
74
|
+
}
|
|
75
|
+
if (contentKey.length !== 32) {
|
|
76
|
+
throw new RangeError('decryptBlob: contentKey must be 32 bytes');
|
|
77
|
+
}
|
|
78
|
+
const headerProbeBytes = new Uint8Array(await blob.slice(0, Math.min(blob.size, 4096)).arrayBuffer());
|
|
79
|
+
let parsed;
|
|
80
|
+
try {
|
|
81
|
+
parsed = parseHeader(headerProbeBytes);
|
|
82
|
+
}
|
|
83
|
+
catch (err) {
|
|
84
|
+
if (err instanceof HeaderError && err.code === 'header_too_short') {
|
|
85
|
+
const allHeaderBytes = new Uint8Array(await blob
|
|
86
|
+
.slice(0, Math.min(blob.size, 4 + 0xffff + 1024))
|
|
87
|
+
.arrayBuffer());
|
|
88
|
+
parsed = parseHeader(allHeaderBytes);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
throw err;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (options.allowedSuites &&
|
|
95
|
+
!options.allowedSuites.includes(parsed.suite)) {
|
|
96
|
+
throw new HeaderError('suite_not_allowed');
|
|
97
|
+
}
|
|
98
|
+
if (parsed.suite !== SUITE.XCHACHA20_POLY1305_V1) {
|
|
99
|
+
throw new HeaderError(`decryptBlob: this entry point only supports xchacha20-poly1305-v1; got suite 0x${parsed.suite.toString(16)}`);
|
|
100
|
+
}
|
|
101
|
+
// MUST verify the header MAC before reading or interpreting any
|
|
102
|
+
// suite_payload bytes — see parseHeader docstring and
|
|
103
|
+
// spec/format-v1.md § "verification order".
|
|
104
|
+
await verifyHeaderMac(parsed, contentKey);
|
|
105
|
+
parseXChaChaV1SuitePayload(parsed.suitePayload);
|
|
106
|
+
const ctx = await createChunkContext(contentKey, parsed.fileId, parsed.totalChunks);
|
|
107
|
+
const plaintextParts = [];
|
|
108
|
+
let cursor = parsed.headerLength;
|
|
109
|
+
let bytesEmitted = 0;
|
|
110
|
+
for (let i = 0; i < parsed.totalChunks; i += 1) {
|
|
111
|
+
if (cursor + LENGTH_PREFIX_BYTES > blob.size) {
|
|
112
|
+
throw new HeaderError('chunk_length_truncated');
|
|
113
|
+
}
|
|
114
|
+
const lenBytes = new Uint8Array(await blob.slice(cursor, cursor + LENGTH_PREFIX_BYTES).arrayBuffer());
|
|
115
|
+
cursor += LENGTH_PREFIX_BYTES;
|
|
116
|
+
const cipherLen = readUint32BE(lenBytes);
|
|
117
|
+
const isFinal = i === parsed.totalChunks - 1;
|
|
118
|
+
const minCipher = 1 + XCHACHA_V1_TAG_BYTES;
|
|
119
|
+
const maxCipher = parsed.chunkSize + XCHACHA_V1_TAG_BYTES;
|
|
120
|
+
if (cipherLen < minCipher || cipherLen > maxCipher) {
|
|
121
|
+
throw new HeaderError('chunk_length_out_of_range');
|
|
122
|
+
}
|
|
123
|
+
if (!isFinal && cipherLen !== maxCipher) {
|
|
124
|
+
throw new HeaderError('non_final_chunk_wrong_size');
|
|
125
|
+
}
|
|
126
|
+
if (cursor + cipherLen > blob.size) {
|
|
127
|
+
throw new HeaderError('chunk_truncated');
|
|
128
|
+
}
|
|
129
|
+
const cipherBytes = new Uint8Array(await blob.slice(cursor, cursor + cipherLen).arrayBuffer());
|
|
130
|
+
cursor += cipherLen;
|
|
131
|
+
const plaintext = await decryptChunk(ctx, i, cipherBytes);
|
|
132
|
+
bytesEmitted += plaintext.length;
|
|
133
|
+
plaintextParts.push(asBlobPart(plaintext));
|
|
134
|
+
onProgress?.((i + 1) / Math.max(parsed.totalChunks, 1));
|
|
135
|
+
}
|
|
136
|
+
if (bytesEmitted !== parsed.plaintextSize) {
|
|
137
|
+
throw new HeaderError('plaintext_size_mismatch');
|
|
138
|
+
}
|
|
139
|
+
if (cursor !== blob.size) {
|
|
140
|
+
throw new HeaderError('trailing_bytes_after_last_chunk');
|
|
141
|
+
}
|
|
142
|
+
if (parsed.totalChunks === 0)
|
|
143
|
+
onProgress?.(1);
|
|
144
|
+
return new Blob(plaintextParts, { type: 'application/octet-stream' });
|
|
145
|
+
}
|
|
146
|
+
export async function encryptBytes(bytes, options) {
|
|
147
|
+
const blob = new Blob([asBlobPart(bytes)]);
|
|
148
|
+
return encryptBlob({ blob, ...options });
|
|
149
|
+
}
|
|
150
|
+
export async function decryptToBytes(options) {
|
|
151
|
+
const blob = await decryptBlob(options);
|
|
152
|
+
return new Uint8Array(await blob.arrayBuffer());
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.js","sourceRoot":"","sources":["../../../src/suites/xchacha-v1/api.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EACL,wBAAwB,EACxB,WAAW,EACX,eAAe,EACf,WAAW,GACZ,MAAM,wBAAwB,CAAA;AAC/B,OAAO,EACL,+BAA+B,EAC/B,oBAAoB,EACpB,kBAAkB,EAClB,YAAY,EACZ,YAAY,EACZ,uBAAuB,EACvB,0BAA0B,GAC3B,MAAM,YAAY,CAAA;AACnB,OAAO,EACL,kBAAkB,EAElB,YAAY,EACZ,gBAAgB,EAEhB,KAAK,GACN,MAAM,yBAAyB,CAAA;AAChC,OAAO,EACL,UAAU,EACV,YAAY,EACZ,QAAQ,GACT,MAAM,4BAA4B,CAAA;AAEnC,MAAM,mBAAmB,GAAG,CAAC,CAAA;AAW7B,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,OAA8B;IAE9B,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,GAAG,OAAO,CA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C,IAAI,EAAE,CAAC;QACzB,MAAM,IAAI,WAAW,CAAC,iCAAiC,CAAC,CAAA;IAC1D,CAAC;IAED,IAAI,MAAM,CAAC,WAAW,KAAK,CAAC;QAAE,UAAU,EAAE,CAAC,CAAC,CAAC,CAAA;IAE7C,OAAO,IAAI,IAAI,CAAC,cAAc,EAAE,EAAE,IAAI,EAAE,0BAA0B,EAAE,CAAC,CAAA;AACvE,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,KAAiB,EACjB,OAA4C;IAE5C,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;IAC1C,OAAO,WAAW,CAAC,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAA;AAC1C,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,OAA8B;IAE9B,MAAM,IAAI,GAAG,MAAM,WAAW,CAAC,OAAO,CAAC,CAAA;IACvC,OAAO,IAAI,UAAU,CAAC,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC,CAAA;AACjD,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cipher suite 0x02: XChaCha20-Poly1305 v1.
|
|
3
|
+
*
|
|
4
|
+
* 256-bit key, 192-bit nonce. Fresh per-file content key. Per-chunk AEAD
|
|
5
|
+
* with the same file_id-bound AAD as the AES-GCM suite. Implementation via
|
|
6
|
+
* libsodium-wrappers-sumo's `crypto_aead_xchacha20poly1305_ietf_*`.
|
|
7
|
+
*
|
|
8
|
+
* Why not secretstream? secretstream is a higher-level construction that
|
|
9
|
+
* embeds its own state machine and chunk count. We want chunk-level random
|
|
10
|
+
* access (for streaming downloads with range requests), so we use the AEAD
|
|
11
|
+
* primitive directly with our own AAD-bound chunk indexing.
|
|
12
|
+
*/
|
|
13
|
+
import { type ParsedHeader } from '../../internal/types.js';
|
|
14
|
+
export declare const XCHACHA_V1_KEY_BYTES = 32;
|
|
15
|
+
export declare const XCHACHA_V1_NONCE_BYTES = 24;
|
|
16
|
+
export declare const XCHACHA_V1_TAG_BYTES = 16;
|
|
17
|
+
/** Suite payload layout: 72-byte secretbox-wrapped key + 24-byte nonce = 96 bytes. */
|
|
18
|
+
export declare const XCHACHA_V1_SUITE_PAYLOAD_LENGTH = 96;
|
|
19
|
+
export interface XChaChaV1SuitePayload {
|
|
20
|
+
wrappedKey: Uint8Array;
|
|
21
|
+
wrapNonce: Uint8Array;
|
|
22
|
+
}
|
|
23
|
+
export declare function buildXChaChaV1SuitePayload(payload: XChaChaV1SuitePayload): Uint8Array;
|
|
24
|
+
export declare function parseXChaChaV1SuitePayload(bytes: Uint8Array): XChaChaV1SuitePayload;
|
|
25
|
+
export declare function generateContentMaterial(): {
|
|
26
|
+
contentKey: Uint8Array;
|
|
27
|
+
fileId: Uint8Array;
|
|
28
|
+
};
|
|
29
|
+
export interface XChaChaV1ChunkContext {
|
|
30
|
+
chunkKey: Uint8Array;
|
|
31
|
+
noncePrefix: Uint8Array;
|
|
32
|
+
totalChunks: number;
|
|
33
|
+
}
|
|
34
|
+
export declare function createChunkContext(contentKey: Uint8Array, fileId: Uint8Array, totalChunks: number): Promise<XChaChaV1ChunkContext>;
|
|
35
|
+
export declare function encryptChunk(ctx: XChaChaV1ChunkContext, chunkIndex: number, plaintext: Uint8Array): Promise<Uint8Array>;
|
|
36
|
+
export declare function decryptChunk(ctx: XChaChaV1ChunkContext, chunkIndex: number, ciphertext: Uint8Array): Promise<Uint8Array>;
|
|
37
|
+
export declare function wrapContentKey(options: {
|
|
38
|
+
contentKey: Uint8Array;
|
|
39
|
+
envelopeKey: Uint8Array;
|
|
40
|
+
}): Promise<{
|
|
41
|
+
wrapped: Uint8Array;
|
|
42
|
+
wrapNonce: Uint8Array;
|
|
43
|
+
}>;
|
|
44
|
+
export declare function unwrapContentKey(options: {
|
|
45
|
+
envelopeKey: Uint8Array;
|
|
46
|
+
wrappedField: Uint8Array;
|
|
47
|
+
wrapNonce: Uint8Array;
|
|
48
|
+
}): Promise<Uint8Array>;
|
|
49
|
+
export declare function getSuitePayloadFromHeader(header: ParsedHeader): XChaChaV1SuitePayload;
|
|
50
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/suites/xchacha-v1/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,EAGL,KAAK,YAAY,EAClB,MAAM,yBAAyB,CAAA;AAMhC,eAAO,MAAM,oBAAoB,KAAK,CAAA;AACtC,eAAO,MAAM,sBAAsB,KAAK,CAAA;AACxC,eAAO,MAAM,oBAAoB,KAAK,CAAA;AAEtC,sFAAsF;AACtF,eAAO,MAAM,+BAA+B,KAAK,CAAA;AAEjD,MAAM,WAAW,qBAAqB;IACpC,UAAU,EAAE,UAAU,CAAA;IACtB,SAAS,EAAE,UAAU,CAAA;CACtB;AAED,wBAAgB,0BAA0B,CACxC,OAAO,EAAE,qBAAqB,GAC7B,UAAU,CAQZ;AAED,wBAAgB,0BAA0B,CACxC,KAAK,EAAE,UAAU,GAChB,qBAAqB,CAQvB;AA8BD,wBAAgB,uBAAuB,IAAI;IACzC,UAAU,EAAE,UAAU,CAAA;IACtB,MAAM,EAAE,UAAU,CAAA;CACnB,CAKA;AAED,MAAM,WAAW,qBAAqB;IACpC,QAAQ,EAAE,UAAU,CAAA;IACpB,WAAW,EAAE,UAAU,CAAA;IACvB,WAAW,EAAE,MAAM,CAAA;CACpB;AAED,wBAAsB,kBAAkB,CACtC,UAAU,EAAE,UAAU,EACtB,MAAM,EAAE,UAAU,EAClB,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,qBAAqB,CAAC,CAIhC;AAED,wBAAsB,YAAY,CAChC,GAAG,EAAE,qBAAqB,EAC1B,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,UAAU,GACpB,OAAO,CAAC,UAAU,CAAC,CAYrB;AAED,wBAAsB,YAAY,CAChC,GAAG,EAAE,qBAAqB,EAC1B,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,UAAU,GACrB,OAAO,CAAC,UAAU,CAAC,CAarB;AAED,wBAAsB,cAAc,CAAC,OAAO,EAAE;IAC5C,UAAU,EAAE,UAAU,CAAA;IACtB,WAAW,EAAE,UAAU,CAAA;CACxB,GAAG,OAAO,CAAC;IAAE,OAAO,EAAE,UAAU,CAAC;IAAC,SAAS,EAAE,UAAU,CAAA;CAAE,CAAC,CAuB1D;AAED,wBAAsB,gBAAgB,CAAC,OAAO,EAAE;IAC9C,WAAW,EAAE,UAAU,CAAA;IACvB,YAAY,EAAE,UAAU,CAAA;IACxB,SAAS,EAAE,UAAU,CAAA;CACtB,GAAG,OAAO,CAAC,UAAU,CAAC,CAuBtB;AAED,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,YAAY,GACnB,qBAAqB,CAEvB"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cipher suite 0x02: XChaCha20-Poly1305 v1.
|
|
3
|
+
*
|
|
4
|
+
* 256-bit key, 192-bit nonce. Fresh per-file content key. Per-chunk AEAD
|
|
5
|
+
* with the same file_id-bound AAD as the AES-GCM suite. Implementation via
|
|
6
|
+
* libsodium-wrappers-sumo's `crypto_aead_xchacha20poly1305_ietf_*`.
|
|
7
|
+
*
|
|
8
|
+
* Why not secretstream? secretstream is a higher-level construction that
|
|
9
|
+
* embeds its own state machine and chunk count. We want chunk-level random
|
|
10
|
+
* access (for streaming downloads with range requests), so we use the AEAD
|
|
11
|
+
* primitive directly with our own AAD-bound chunk indexing.
|
|
12
|
+
*/
|
|
13
|
+
import { buildChunkAad } from '../../format/header.js';
|
|
14
|
+
import { HKDF_INFO, HEADER_SIZES, } from '../../internal/types.js';
|
|
15
|
+
import { concatBytes, uint64BE } from '../../internal/encoding.js';
|
|
16
|
+
import { hkdfSha256 } from '../../internal/hkdf.js';
|
|
17
|
+
import { randomBytes } from '../../internal/runtime.js';
|
|
18
|
+
import { getSodium } from '../../internal/sodium.js';
|
|
19
|
+
export const XCHACHA_V1_KEY_BYTES = 32;
|
|
20
|
+
export const XCHACHA_V1_NONCE_BYTES = 24;
|
|
21
|
+
export const XCHACHA_V1_TAG_BYTES = 16;
|
|
22
|
+
/** Suite payload layout: 72-byte secretbox-wrapped key + 24-byte nonce = 96 bytes. */
|
|
23
|
+
export const XCHACHA_V1_SUITE_PAYLOAD_LENGTH = 96;
|
|
24
|
+
export function buildXChaChaV1SuitePayload(payload) {
|
|
25
|
+
if (payload.wrappedKey.length !== 72) {
|
|
26
|
+
throw new RangeError('xchacha-v1: wrappedKey must be 72 bytes');
|
|
27
|
+
}
|
|
28
|
+
if (payload.wrapNonce.length !== XCHACHA_V1_NONCE_BYTES) {
|
|
29
|
+
throw new RangeError('xchacha-v1: wrapNonce must be 24 bytes');
|
|
30
|
+
}
|
|
31
|
+
return concatBytes([payload.wrappedKey, payload.wrapNonce]);
|
|
32
|
+
}
|
|
33
|
+
export function parseXChaChaV1SuitePayload(bytes) {
|
|
34
|
+
if (bytes.length !== XCHACHA_V1_SUITE_PAYLOAD_LENGTH) {
|
|
35
|
+
throw new RangeError('xchacha-v1: suite payload must be 96 bytes');
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
wrappedKey: bytes.slice(0, 72),
|
|
39
|
+
wrapNonce: bytes.slice(72, 96),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
async function deriveChunkKey(contentKey, fileId) {
|
|
43
|
+
return hkdfSha256({
|
|
44
|
+
ikm: contentKey,
|
|
45
|
+
salt: fileId,
|
|
46
|
+
info: HKDF_INFO.XCHACHA_CHUNK_KEY,
|
|
47
|
+
length: XCHACHA_V1_KEY_BYTES,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
async function deriveNoncePrefix(fileId) {
|
|
51
|
+
// XChaCha's 24-byte nonce: 16-byte file-derived prefix + 8-byte counter.
|
|
52
|
+
return hkdfSha256({
|
|
53
|
+
ikm: fileId,
|
|
54
|
+
info: HKDF_INFO.XCHACHA_NONCE_PREFIX,
|
|
55
|
+
length: 16,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
function buildNonce(noncePrefix, chunkIndex) {
|
|
59
|
+
const nonce = new Uint8Array(XCHACHA_V1_NONCE_BYTES);
|
|
60
|
+
nonce.set(noncePrefix, 0);
|
|
61
|
+
nonce.set(uint64BE(chunkIndex), 16);
|
|
62
|
+
return nonce;
|
|
63
|
+
}
|
|
64
|
+
export function generateContentMaterial() {
|
|
65
|
+
return {
|
|
66
|
+
contentKey: randomBytes(XCHACHA_V1_KEY_BYTES),
|
|
67
|
+
fileId: randomBytes(HEADER_SIZES.FILE_ID),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
export async function createChunkContext(contentKey, fileId, totalChunks) {
|
|
71
|
+
const chunkKey = await deriveChunkKey(contentKey, fileId);
|
|
72
|
+
const noncePrefix = await deriveNoncePrefix(fileId);
|
|
73
|
+
return { chunkKey, noncePrefix, totalChunks };
|
|
74
|
+
}
|
|
75
|
+
export async function encryptChunk(ctx, chunkIndex, plaintext) {
|
|
76
|
+
const sodium = await getSodium();
|
|
77
|
+
const isFinal = chunkIndex === ctx.totalChunks - 1;
|
|
78
|
+
const aad = buildChunkAad(chunkIndex, ctx.totalChunks, isFinal);
|
|
79
|
+
const nonce = buildNonce(ctx.noncePrefix, chunkIndex);
|
|
80
|
+
return sodium.crypto_aead_xchacha20poly1305_ietf_encrypt(plaintext, aad, null, nonce, ctx.chunkKey);
|
|
81
|
+
}
|
|
82
|
+
export async function decryptChunk(ctx, chunkIndex, ciphertext) {
|
|
83
|
+
const sodium = await getSodium();
|
|
84
|
+
const isFinal = chunkIndex === ctx.totalChunks - 1;
|
|
85
|
+
const aad = buildChunkAad(chunkIndex, ctx.totalChunks, isFinal);
|
|
86
|
+
const nonce = buildNonce(ctx.noncePrefix, chunkIndex);
|
|
87
|
+
const result = sodium.crypto_aead_xchacha20poly1305_ietf_decrypt(null, ciphertext, aad, nonce, ctx.chunkKey);
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
export async function wrapContentKey(options) {
|
|
91
|
+
const sodium = await getSodium();
|
|
92
|
+
const { contentKey, envelopeKey } = options;
|
|
93
|
+
if (contentKey.length !== XCHACHA_V1_KEY_BYTES) {
|
|
94
|
+
throw new RangeError('wrapContentKey: contentKey must be 32 bytes');
|
|
95
|
+
}
|
|
96
|
+
if (envelopeKey.length !== XCHACHA_V1_KEY_BYTES) {
|
|
97
|
+
throw new RangeError('wrapContentKey: envelopeKey must be 32 bytes');
|
|
98
|
+
}
|
|
99
|
+
const wrapNonce = randomBytes(XCHACHA_V1_NONCE_BYTES);
|
|
100
|
+
// crypto_secretbox_easy: outputs 16-byte poly1305 tag + ciphertext = 48 bytes
|
|
101
|
+
const wrapped = sodium.crypto_secretbox_easy(contentKey, wrapNonce, envelopeKey);
|
|
102
|
+
if (wrapped.length !== 48) {
|
|
103
|
+
throw new Error('wrapContentKey: unexpected wrapped length');
|
|
104
|
+
}
|
|
105
|
+
// Pad to 72 bytes for fixed suite_payload size
|
|
106
|
+
const padded = new Uint8Array(72);
|
|
107
|
+
padded.set(wrapped, 0);
|
|
108
|
+
return { wrapped: padded, wrapNonce };
|
|
109
|
+
}
|
|
110
|
+
export async function unwrapContentKey(options) {
|
|
111
|
+
const sodium = await getSodium();
|
|
112
|
+
const { envelopeKey, wrappedField, wrapNonce } = options;
|
|
113
|
+
if (envelopeKey.length !== XCHACHA_V1_KEY_BYTES) {
|
|
114
|
+
throw new RangeError('unwrapContentKey: envelopeKey must be 32 bytes');
|
|
115
|
+
}
|
|
116
|
+
if (wrappedField.length !== 72) {
|
|
117
|
+
throw new RangeError('unwrapContentKey: wrappedField must be 72 bytes');
|
|
118
|
+
}
|
|
119
|
+
if (wrapNonce.length !== XCHACHA_V1_NONCE_BYTES) {
|
|
120
|
+
throw new RangeError('unwrapContentKey: wrapNonce must be 24 bytes');
|
|
121
|
+
}
|
|
122
|
+
// First 48 bytes are tag+key, the rest is reserved zeros.
|
|
123
|
+
const wrapped = wrappedField.slice(0, 48);
|
|
124
|
+
const unwrapped = sodium.crypto_secretbox_open_easy(wrapped, wrapNonce, envelopeKey);
|
|
125
|
+
if (!unwrapped) {
|
|
126
|
+
throw new Error('unwrapContentKey: unwrap failed (wrong envelope key?)');
|
|
127
|
+
}
|
|
128
|
+
return unwrapped;
|
|
129
|
+
}
|
|
130
|
+
export function getSuitePayloadFromHeader(header) {
|
|
131
|
+
return parseXChaChaV1SuitePayload(header.suitePayload);
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/suites/xchacha-v1/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AACtD,OAAO,EACL,SAAS,EACT,YAAY,GAEb,MAAM,yBAAyB,CAAA;AAChC,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAA;AAClE,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AAEpD,MAAM,CAAC,MAAM,oBAAoB,GAAG,EAAE,CAAA;AACtC,MAAM,CAAC,MAAM,sBAAsB,GAAG,EAAE,CAAA;AACxC,MAAM,CAAC,MAAM,oBAAoB,GAAG,EAAE,CAAA;AAEtC,sFAAsF;AACtF,MAAM,CAAC,MAAM,+BAA+B,GAAG,EAAE,CAAA;AAOjD,MAAM,UAAU,0BAA0B,CACxC,OAA8B;IAE9B,IAAI,OAAO,CAAC,UAAU,CAAC,MAAM,KAAK,EAA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K,UAAU,gBAAgB,CAAC,OAItC;IACC,MAAM,MAAM,GAAG,MAAM,SAAS,EAAE,CAAA;IAChC,MAAM,EAAE,WAAW,EAAE,YAAY,EAAE,SAAS,EAAE,GAAG,OAAO,CAAA;IACxD,IAAI,WAAW,CAAC,MAAM,KAAK,oBAAoB,EAAE,CAAC;QAChD,MAAM,IAAI,UAAU,CAAC,gDAAgD,CAAC,CAAA;IACxE,CAAC;IACD,IAAI,YAAY,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;QAC/B,MAAM,IAAI,UAAU,CAAC,iDAAiD,CAAC,CAAA;IACzE,CAAC;IACD,IAAI,SAAS,CAAC,MAAM,KAAK,sBAAsB,EAAE,CAAC;QAChD,MAAM,IAAI,UAAU,CAAC,8CAA8C,CAAC,CAAA;IACtE,CAAC;IACD,0DAA0D;IAC1D,MAAM,OAAO,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAA;IACzC,MAAM,SAAS,GAAG,MAAM,CAAC,0BAA0B,CACjD,OAAO,EACP,SAAS,EACT,WAAW,CACZ,CAAA;IACD,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAA;IAC1E,CAAC;IACD,OAAO,SAAS,CAAA;AAClB,CAAC;AAED,MAAM,UAAU,yBAAyB,CACvC,MAAoB;IAEpB,OAAO,0BAA0B,CAAC,MAAM,CAAC,YAAY,CAAC,CAAA;AACxD,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shieldfive/crypto",
|
|
3
|
+
"version": "1.0.0-alpha.11",
|
|
4
|
+
"description": "Client-side post-quantum hybrid encryption for cloud storage. The cryptographic core of ShieldFive.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"homepage": "https://github.com/shieldfive/crypto",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/shieldfive/crypto.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/shieldfive/crypto/issues",
|
|
14
|
+
"email": "security@shieldfive.com"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"encryption",
|
|
18
|
+
"zero-knowledge",
|
|
19
|
+
"end-to-end-encryption",
|
|
20
|
+
"post-quantum",
|
|
21
|
+
"ml-kem",
|
|
22
|
+
"kyber",
|
|
23
|
+
"xchacha20-poly1305",
|
|
24
|
+
"aes-gcm",
|
|
25
|
+
"webcrypto",
|
|
26
|
+
"cloud-storage"
|
|
27
|
+
],
|
|
28
|
+
"author": "Cho Garcia <security@shieldfive.com>",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"default": "./dist/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./package.json": "./package.json",
|
|
35
|
+
"./aes-gcm-v1": {
|
|
36
|
+
"types": "./dist/suites/aes-gcm-v1/api.d.ts",
|
|
37
|
+
"default": "./dist/suites/aes-gcm-v1/api.js"
|
|
38
|
+
},
|
|
39
|
+
"./aes-gcm-v2": {
|
|
40
|
+
"types": "./dist/suites/aes-gcm-v2/api.d.ts",
|
|
41
|
+
"default": "./dist/suites/aes-gcm-v2/api.js"
|
|
42
|
+
},
|
|
43
|
+
"./xchacha-v1": {
|
|
44
|
+
"types": "./dist/suites/xchacha-v1/api.d.ts",
|
|
45
|
+
"default": "./dist/suites/xchacha-v1/api.js"
|
|
46
|
+
},
|
|
47
|
+
"./pq-hybrid-v1": {
|
|
48
|
+
"types": "./dist/suites/pq-hybrid-v1/api.d.ts",
|
|
49
|
+
"default": "./dist/suites/pq-hybrid-v1/api.js"
|
|
50
|
+
},
|
|
51
|
+
"./legacy-v0": {
|
|
52
|
+
"types": "./dist/suites/aes-gcm-v0/api.d.ts",
|
|
53
|
+
"default": "./dist/suites/aes-gcm-v0/api.js"
|
|
54
|
+
},
|
|
55
|
+
"./streams/aes-gcm-v1": {
|
|
56
|
+
"types": "./dist/streams/aes-gcm-v1.d.ts",
|
|
57
|
+
"default": "./dist/streams/aes-gcm-v1.js"
|
|
58
|
+
},
|
|
59
|
+
"./streams/pq-hybrid-v1": {
|
|
60
|
+
"types": "./dist/streams/pq-hybrid-v1.d.ts",
|
|
61
|
+
"default": "./dist/streams/pq-hybrid-v1.js"
|
|
62
|
+
},
|
|
63
|
+
"./kdf/argon2id": {
|
|
64
|
+
"types": "./dist/kdf/argon2id.d.ts",
|
|
65
|
+
"default": "./dist/kdf/argon2id.js"
|
|
66
|
+
},
|
|
67
|
+
"./identity": {
|
|
68
|
+
"types": "./dist/identity/index.d.ts",
|
|
69
|
+
"default": "./dist/identity/index.js"
|
|
70
|
+
},
|
|
71
|
+
"./migration/v0": {
|
|
72
|
+
"types": "./dist/migration/v0-bridge.d.ts",
|
|
73
|
+
"default": "./dist/migration/v0-bridge.js"
|
|
74
|
+
},
|
|
75
|
+
"./format": {
|
|
76
|
+
"types": "./dist/format/header.d.ts",
|
|
77
|
+
"default": "./dist/format/header.js"
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
"files": [
|
|
81
|
+
"dist",
|
|
82
|
+
"src",
|
|
83
|
+
"spec",
|
|
84
|
+
"workers",
|
|
85
|
+
"README.md",
|
|
86
|
+
"SECURITY.md",
|
|
87
|
+
"LICENSE",
|
|
88
|
+
"CHANGELOG.md"
|
|
89
|
+
],
|
|
90
|
+
"engines": {
|
|
91
|
+
"node": ">=20"
|
|
92
|
+
},
|
|
93
|
+
"sideEffects": false,
|
|
94
|
+
"scripts": {
|
|
95
|
+
"build": "tsc -p tsconfig.json",
|
|
96
|
+
"clean": "rm -rf dist",
|
|
97
|
+
"lint": "eslint src tests",
|
|
98
|
+
"test": "node --experimental-vm-modules --test --import tsx tests/**/*.test.ts",
|
|
99
|
+
"test:unit": "node --experimental-vm-modules --test --import tsx tests/unit/*.test.ts",
|
|
100
|
+
"test:integration": "node --experimental-vm-modules --test --import tsx tests/integration/*.test.ts",
|
|
101
|
+
"format": "prettier --write \"src/**/*.ts\" \"tests/**/*.ts\""
|
|
102
|
+
},
|
|
103
|
+
"peerDependencies": {
|
|
104
|
+
"libsodium-wrappers-sumo": "^0.8.2"
|
|
105
|
+
},
|
|
106
|
+
"peerDependenciesMeta": {
|
|
107
|
+
"libsodium-wrappers-sumo": {
|
|
108
|
+
"optional": true
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
"dependencies": {
|
|
112
|
+
"@noble/curves": "^2.2.0",
|
|
113
|
+
"@noble/post-quantum": "^0.6.0"
|
|
114
|
+
},
|
|
115
|
+
"devDependencies": {
|
|
116
|
+
"@eslint/js": "^10.0.1",
|
|
117
|
+
"@types/node": "^22.10.0",
|
|
118
|
+
"eslint": "^10.4.0",
|
|
119
|
+
"libsodium-wrappers-sumo": "^0.8.2",
|
|
120
|
+
"tsx": "^4.20.0",
|
|
121
|
+
"typescript": "^5.7.2",
|
|
122
|
+
"typescript-eslint": "^8.59.4"
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# ShieldFive Crypto File Format v0 (legacy)
|
|
2
|
+
|
|
3
|
+
This document describes the **legacy** ShieldFive production file format,
|
|
4
|
+
present in production from project inception through the v1 transition.
|
|
5
|
+
It is included here so that v1-aware readers can read v0 files indefinitely.
|
|
6
|
+
|
|
7
|
+
**v0 is read-only as of the v1 release.** New encryptions MUST use v1.
|
|
8
|
+
|
|
9
|
+
## Wire format
|
|
10
|
+
|
|
11
|
+
v0 files are stored as the concatenation of independently-encrypted
|
|
12
|
+
AES-256-GCM chunks:
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
v0_file := chunk_0 || chunk_1 || ... || chunk_n
|
|
16
|
+
chunk_i := AES-GCM(key=content_key, iv=iv_i, aad=∅, plaintext=plain_i)
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Each chunk's ciphertext includes its 16-byte GCM authentication tag.
|
|
20
|
+
|
|
21
|
+
There is **no on-disk header**. Decryption requires the following
|
|
22
|
+
out-of-band metadata, retrieved from the application database:
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
cipher_version = 1 (uint, currently always 1)
|
|
26
|
+
cipher_chunk_size = 5*1024*1024 or 8*1024*1024 (uint, plaintext bytes per chunk)
|
|
27
|
+
cipher_nonce_prefix = base64(4 random bytes) (per-file)
|
|
28
|
+
csk_wrapped = base64(AES-GCM-wrapped 32-byte content key)
|
|
29
|
+
csk_iv = base64(12-byte IV used for wrapping)
|
|
30
|
+
cipher_parts_sha1 = [SHA-1(chunk_0), ..., SHA-1(chunk_n)] (storage integrity)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The IV for chunk `i` is:
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
iv_i := nonce_prefix (4 bytes) || uint64_be(i)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The content key is unwrapped with the parent folder/root key using
|
|
40
|
+
AES-GCM. The wrapped key and wrap IV are stored in the database, not in
|
|
41
|
+
the file.
|
|
42
|
+
|
|
43
|
+
## Limitations of v0 (addressed in v1)
|
|
44
|
+
|
|
45
|
+
These are the reasons v0 is being retired in favor of v1:
|
|
46
|
+
|
|
47
|
+
1. **No on-disk version byte.** A v0 file is indistinguishable from
|
|
48
|
+
arbitrary ciphertext without out-of-band context.
|
|
49
|
+
2. **No file_id binding.** Chunks can be spliced between two files
|
|
50
|
+
encrypted with the same content key (which should not occur, but is
|
|
51
|
+
not structurally prevented).
|
|
52
|
+
3. **No truncation detection.** Trailing chunks can be removed and the
|
|
53
|
+
reader will produce a valid (truncated) plaintext. v0 deployments
|
|
54
|
+
detect this at the application layer using `cipher_parts_sha1`, not in
|
|
55
|
+
the AEAD.
|
|
56
|
+
4. **No AAD on chunks.** Chunk index, total chunks, and is-final flag are
|
|
57
|
+
not authenticated. Reordering is detected only because the IV depends
|
|
58
|
+
on the index, but reordering does not produce a clear error message.
|
|
59
|
+
5. **No post-quantum protection.**
|
|
60
|
+
6. **Suite is hardcoded.** Future cipher upgrades require a new
|
|
61
|
+
`cipher_version` and a database migration, not a per-file decision.
|
|
62
|
+
|
|
63
|
+
## Migration path
|
|
64
|
+
|
|
65
|
+
v1-aware applications:
|
|
66
|
+
|
|
67
|
+
- MUST be able to decrypt v0 files indefinitely.
|
|
68
|
+
- SHOULD migrate stored v0 files to v1 in the background as users
|
|
69
|
+
download/re-upload them or via batch re-encryption.
|
|
70
|
+
- MUST NOT issue new v0 writes once a v1 deployment is live.
|
|
71
|
+
|
|
72
|
+
The reference v0 implementation lives in `src/suites/aes-gcm-v0/` and
|
|
73
|
+
exposes only a `decryptV0` function. There is no `encryptV0` exported
|
|
74
|
+
symbol; this is intentional.
|
|
75
|
+
|
|
76
|
+
## Security note
|
|
77
|
+
|
|
78
|
+
v0 is not broken. It is an older design with weaker structural guarantees
|
|
79
|
+
than v1. Files encrypted with v0 remain confidential under the original
|
|
80
|
+
threat model (server compromise, network interception). The v1 upgrade
|
|
81
|
+
adds defense-in-depth, not a fix to a known v0 vulnerability.
|