@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.
Files changed (124) hide show
  1. package/CHANGELOG.md +228 -0
  2. package/LICENSE +201 -0
  3. package/README.md +275 -0
  4. package/SECURITY.md +130 -0
  5. package/dist/format/header.d.ts +73 -0
  6. package/dist/format/header.d.ts.map +1 -0
  7. package/dist/format/header.js +230 -0
  8. package/dist/format/header.js.map +1 -0
  9. package/dist/identity/index.d.ts +119 -0
  10. package/dist/identity/index.d.ts.map +1 -0
  11. package/dist/identity/index.js +217 -0
  12. package/dist/identity/index.js.map +1 -0
  13. package/dist/identity/sign.d.ts +92 -0
  14. package/dist/identity/sign.d.ts.map +1 -0
  15. package/dist/identity/sign.js +182 -0
  16. package/dist/identity/sign.js.map +1 -0
  17. package/dist/index.d.ts +58 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +146 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/internal/encoding.d.ts +33 -0
  22. package/dist/internal/encoding.d.ts.map +1 -0
  23. package/dist/internal/encoding.js +135 -0
  24. package/dist/internal/encoding.js.map +1 -0
  25. package/dist/internal/hkdf.d.ts +14 -0
  26. package/dist/internal/hkdf.d.ts.map +1 -0
  27. package/dist/internal/hkdf.js +27 -0
  28. package/dist/internal/hkdf.js.map +1 -0
  29. package/dist/internal/hmac.d.ts +9 -0
  30. package/dist/internal/hmac.d.ts.map +1 -0
  31. package/dist/internal/hmac.js +18 -0
  32. package/dist/internal/hmac.js.map +1 -0
  33. package/dist/internal/runtime.d.ts +29 -0
  34. package/dist/internal/runtime.d.ts.map +1 -0
  35. package/dist/internal/runtime.js +67 -0
  36. package/dist/internal/runtime.js.map +1 -0
  37. package/dist/internal/sodium.d.ts +12 -0
  38. package/dist/internal/sodium.d.ts.map +1 -0
  39. package/dist/internal/sodium.js +34 -0
  40. package/dist/internal/sodium.js.map +1 -0
  41. package/dist/internal/types.d.ts +98 -0
  42. package/dist/internal/types.d.ts.map +1 -0
  43. package/dist/internal/types.js +70 -0
  44. package/dist/internal/types.js.map +1 -0
  45. package/dist/kdf/argon2id.d.ts +72 -0
  46. package/dist/kdf/argon2id.d.ts.map +1 -0
  47. package/dist/kdf/argon2id.js +98 -0
  48. package/dist/kdf/argon2id.js.map +1 -0
  49. package/dist/migration/v0-bridge.d.ts +94 -0
  50. package/dist/migration/v0-bridge.d.ts.map +1 -0
  51. package/dist/migration/v0-bridge.js +203 -0
  52. package/dist/migration/v0-bridge.js.map +1 -0
  53. package/dist/streams/aes-gcm-v1.d.ts +144 -0
  54. package/dist/streams/aes-gcm-v1.d.ts.map +1 -0
  55. package/dist/streams/aes-gcm-v1.js +456 -0
  56. package/dist/streams/aes-gcm-v1.js.map +1 -0
  57. package/dist/streams/pq-hybrid-v1.d.ts +219 -0
  58. package/dist/streams/pq-hybrid-v1.d.ts.map +1 -0
  59. package/dist/streams/pq-hybrid-v1.js +473 -0
  60. package/dist/streams/pq-hybrid-v1.js.map +1 -0
  61. package/dist/suites/aes-gcm-v0/api.d.ts +48 -0
  62. package/dist/suites/aes-gcm-v0/api.d.ts.map +1 -0
  63. package/dist/suites/aes-gcm-v0/api.js +110 -0
  64. package/dist/suites/aes-gcm-v0/api.js.map +1 -0
  65. package/dist/suites/aes-gcm-v1/api.d.ts +54 -0
  66. package/dist/suites/aes-gcm-v1/api.d.ts.map +1 -0
  67. package/dist/suites/aes-gcm-v1/api.js +181 -0
  68. package/dist/suites/aes-gcm-v1/api.js.map +1 -0
  69. package/dist/suites/aes-gcm-v1/index.d.ts +59 -0
  70. package/dist/suites/aes-gcm-v1/index.d.ts.map +1 -0
  71. package/dist/suites/aes-gcm-v1/index.js +177 -0
  72. package/dist/suites/aes-gcm-v1/index.js.map +1 -0
  73. package/dist/suites/aes-gcm-v2/api.d.ts +44 -0
  74. package/dist/suites/aes-gcm-v2/api.d.ts.map +1 -0
  75. package/dist/suites/aes-gcm-v2/api.js +162 -0
  76. package/dist/suites/aes-gcm-v2/api.js.map +1 -0
  77. package/dist/suites/aes-gcm-v2/index.d.ts +63 -0
  78. package/dist/suites/aes-gcm-v2/index.d.ts.map +1 -0
  79. package/dist/suites/aes-gcm-v2/index.js +193 -0
  80. package/dist/suites/aes-gcm-v2/index.js.map +1 -0
  81. package/dist/suites/pq-hybrid-v1/api.d.ts +67 -0
  82. package/dist/suites/pq-hybrid-v1/api.d.ts.map +1 -0
  83. package/dist/suites/pq-hybrid-v1/api.js +190 -0
  84. package/dist/suites/pq-hybrid-v1/api.js.map +1 -0
  85. package/dist/suites/pq-hybrid-v1/index.d.ts +101 -0
  86. package/dist/suites/pq-hybrid-v1/index.d.ts.map +1 -0
  87. package/dist/suites/pq-hybrid-v1/index.js +225 -0
  88. package/dist/suites/pq-hybrid-v1/index.js.map +1 -0
  89. package/dist/suites/xchacha-v1/api.d.ts +29 -0
  90. package/dist/suites/xchacha-v1/api.d.ts.map +1 -0
  91. package/dist/suites/xchacha-v1/api.js +154 -0
  92. package/dist/suites/xchacha-v1/api.js.map +1 -0
  93. package/dist/suites/xchacha-v1/index.d.ts +50 -0
  94. package/dist/suites/xchacha-v1/index.d.ts.map +1 -0
  95. package/dist/suites/xchacha-v1/index.js +133 -0
  96. package/dist/suites/xchacha-v1/index.js.map +1 -0
  97. package/package.json +124 -0
  98. package/spec/format-v0.md +81 -0
  99. package/spec/format-v1.md +400 -0
  100. package/spec/key-derivation.md +167 -0
  101. package/spec/threat-model.md +242 -0
  102. package/src/format/header.ts +316 -0
  103. package/src/identity/index.ts +371 -0
  104. package/src/identity/sign.ts +221 -0
  105. package/src/index.ts +224 -0
  106. package/src/internal/encoding.ts +152 -0
  107. package/src/internal/hkdf.ts +47 -0
  108. package/src/internal/hmac.ts +33 -0
  109. package/src/internal/runtime.ts +80 -0
  110. package/src/internal/sodium.ts +38 -0
  111. package/src/internal/types.ts +134 -0
  112. package/src/kdf/argon2id.ts +165 -0
  113. package/src/migration/v0-bridge.ts +347 -0
  114. package/src/streams/aes-gcm-v1.ts +647 -0
  115. package/src/streams/pq-hybrid-v1.ts +722 -0
  116. package/src/suites/aes-gcm-v0/api.ts +155 -0
  117. package/src/suites/aes-gcm-v1/api.ts +292 -0
  118. package/src/suites/aes-gcm-v1/index.ts +269 -0
  119. package/src/suites/aes-gcm-v2/api.ts +268 -0
  120. package/src/suites/aes-gcm-v2/index.ts +289 -0
  121. package/src/suites/pq-hybrid-v1/api.ts +299 -0
  122. package/src/suites/pq-hybrid-v1/index.ts +343 -0
  123. package/src/suites/xchacha-v1/api.ts +254 -0
  124. package/src/suites/xchacha-v1/index.ts +213 -0
@@ -0,0 +1,162 @@
1
+ /**
2
+ * High-level encrypt/decrypt API for the AES-256-GCM v2 suite.
3
+ *
4
+ * Identical glue to the v1 API (header construction + chunked AEAD); the
5
+ * difference is the suite ID and the underlying nonce-prefix width — see
6
+ * `./index.ts` for the v2 rationale.
7
+ */
8
+ import { buildAuthenticatedHeader, parseHeader, verifyHeaderMac, HeaderError, } from '../../format/header.js';
9
+ import { AES_GCM_V2_SUITE_PAYLOAD_LENGTH, AES_GCM_V2_TAG_BYTES, createChunkContext, decryptChunk, encryptChunk, generateContentMaterial, parseAesGcmV2SuitePayload, } from './index.js';
10
+ import { DEFAULT_CHUNK_SIZE, HEADER_SIZES, MAX_TOTAL_CHUNKS, SUITE, } from '../../internal/types.js';
11
+ import { asBlobPart, readUint32BE, uint32BE, } from '../../internal/encoding.js';
12
+ const LENGTH_PREFIX_BYTES = 4;
13
+ /**
14
+ * Encrypt a Blob/File with the AES-256-GCM v2 suite.
15
+ * Returns the encrypted blob (header + chunks), plus the content key and
16
+ * file id so the caller can wrap/store them externally.
17
+ */
18
+ export async function encryptBlob(options) {
19
+ const { blob, onProgress } = options;
20
+ if (!(blob instanceof Blob)) {
21
+ throw new TypeError('encryptBlob: blob must be a Blob or File');
22
+ }
23
+ const chunkSize = options.chunkSize ?? DEFAULT_CHUNK_SIZE;
24
+ if (!Number.isSafeInteger(chunkSize) || chunkSize <= 0) {
25
+ throw new RangeError('encryptBlob: chunkSize must be a positive integer');
26
+ }
27
+ const plaintextSize = blob.size;
28
+ const totalChunks = plaintextSize === 0 ? 0 : Math.ceil(plaintextSize / chunkSize);
29
+ if (totalChunks > MAX_TOTAL_CHUNKS) {
30
+ throw new RangeError('encryptBlob: file would exceed total_chunks limit');
31
+ }
32
+ const generated = generateContentMaterial();
33
+ const contentKey = options.contentKey ?? generated.contentKey;
34
+ const fileId = options.fileId ?? generated.fileId;
35
+ if (contentKey.length !== 32) {
36
+ throw new RangeError('encryptBlob: contentKey must be 32 bytes');
37
+ }
38
+ if (fileId.length !== HEADER_SIZES.FILE_ID) {
39
+ throw new RangeError('encryptBlob: fileId must be 16 bytes');
40
+ }
41
+ const suitePayload = options.suitePayloadOverride ??
42
+ new Uint8Array(AES_GCM_V2_SUITE_PAYLOAD_LENGTH);
43
+ if (suitePayload.length !== AES_GCM_V2_SUITE_PAYLOAD_LENGTH) {
44
+ throw new RangeError(`encryptBlob: suitePayloadOverride must be ${AES_GCM_V2_SUITE_PAYLOAD_LENGTH} bytes`);
45
+ }
46
+ const header = await buildAuthenticatedHeader({
47
+ suite: SUITE.AES_256_GCM_V2,
48
+ fileId,
49
+ chunkSize,
50
+ totalChunks,
51
+ plaintextSize,
52
+ suitePayload,
53
+ }, contentKey);
54
+ const ctx = await createChunkContext(contentKey, fileId, totalChunks);
55
+ const parts = [asBlobPart(header)];
56
+ for (let i = 0; i < totalChunks; i += 1) {
57
+ const start = i * chunkSize;
58
+ const end = Math.min(start + chunkSize, plaintextSize);
59
+ const plaintextBytes = new Uint8Array(await blob.slice(start, end).arrayBuffer());
60
+ const ciphertext = await encryptChunk(ctx, i, plaintextBytes);
61
+ parts.push(asBlobPart(uint32BE(ciphertext.length)));
62
+ parts.push(asBlobPart(ciphertext));
63
+ onProgress?.((i + 1) / Math.max(totalChunks, 1));
64
+ }
65
+ if (totalChunks === 0)
66
+ onProgress?.(1);
67
+ return {
68
+ blob: new Blob(parts, { type: 'application/octet-stream' }),
69
+ suite: SUITE.AES_256_GCM_V2,
70
+ fileId,
71
+ chunkSize,
72
+ totalChunks,
73
+ plaintextSize,
74
+ contentKey,
75
+ };
76
+ }
77
+ /**
78
+ * Decrypt a v2 blob produced by `encryptBlob`.
79
+ */
80
+ export async function decryptBlob(options) {
81
+ const { blob, contentKey, onProgress } = options;
82
+ if (!(blob instanceof Blob)) {
83
+ throw new TypeError('decryptBlob: blob must be a Blob or File');
84
+ }
85
+ if (contentKey.length !== 32) {
86
+ throw new RangeError('decryptBlob: contentKey must be 32 bytes');
87
+ }
88
+ const headerProbeBytes = new Uint8Array(await blob.slice(0, Math.min(blob.size, 4096)).arrayBuffer());
89
+ let parsed;
90
+ try {
91
+ parsed = parseHeader(headerProbeBytes);
92
+ }
93
+ catch (err) {
94
+ if (err instanceof HeaderError && err.code === 'header_too_short') {
95
+ const allHeaderBytes = new Uint8Array(await blob
96
+ .slice(0, Math.min(blob.size, 4 + 0xffff + 1024))
97
+ .arrayBuffer());
98
+ parsed = parseHeader(allHeaderBytes);
99
+ }
100
+ else {
101
+ throw err;
102
+ }
103
+ }
104
+ if (options.allowedSuites &&
105
+ !options.allowedSuites.includes(parsed.suite)) {
106
+ throw new HeaderError('suite_not_allowed');
107
+ }
108
+ if (parsed.suite !== SUITE.AES_256_GCM_V2) {
109
+ throw new HeaderError(`decryptBlob: this entry point only supports aes-256-gcm-v2; got suite 0x${parsed.suite.toString(16)}`);
110
+ }
111
+ await verifyHeaderMac(parsed, contentKey);
112
+ parseAesGcmV2SuitePayload(parsed.suitePayload);
113
+ const ctx = await createChunkContext(contentKey, parsed.fileId, parsed.totalChunks);
114
+ const plaintextParts = [];
115
+ let cursor = parsed.headerLength;
116
+ let bytesEmitted = 0;
117
+ for (let i = 0; i < parsed.totalChunks; i += 1) {
118
+ if (cursor + LENGTH_PREFIX_BYTES > blob.size) {
119
+ throw new HeaderError('chunk_length_truncated');
120
+ }
121
+ const lenBytes = new Uint8Array(await blob.slice(cursor, cursor + LENGTH_PREFIX_BYTES).arrayBuffer());
122
+ cursor += LENGTH_PREFIX_BYTES;
123
+ const cipherLen = readUint32BE(lenBytes);
124
+ const isFinal = i === parsed.totalChunks - 1;
125
+ const minCipher = 1 + AES_GCM_V2_TAG_BYTES;
126
+ const maxCipher = parsed.chunkSize + AES_GCM_V2_TAG_BYTES;
127
+ if (cipherLen < minCipher || cipherLen > maxCipher) {
128
+ throw new HeaderError('chunk_length_out_of_range');
129
+ }
130
+ if (!isFinal && cipherLen !== maxCipher) {
131
+ throw new HeaderError('non_final_chunk_wrong_size');
132
+ }
133
+ if (cursor + cipherLen > blob.size) {
134
+ throw new HeaderError('chunk_truncated');
135
+ }
136
+ const cipherBytes = new Uint8Array(await blob.slice(cursor, cursor + cipherLen).arrayBuffer());
137
+ cursor += cipherLen;
138
+ const plaintext = await decryptChunk(ctx, i, cipherBytes);
139
+ bytesEmitted += plaintext.length;
140
+ plaintextParts.push(asBlobPart(plaintext));
141
+ onProgress?.((i + 1) / Math.max(parsed.totalChunks, 1));
142
+ }
143
+ if (bytesEmitted !== parsed.plaintextSize) {
144
+ throw new HeaderError('plaintext_size_mismatch');
145
+ }
146
+ if (cursor !== blob.size) {
147
+ throw new HeaderError('trailing_bytes_after_last_chunk');
148
+ }
149
+ if (parsed.totalChunks === 0)
150
+ onProgress?.(1);
151
+ return new Blob(plaintextParts, { type: 'application/octet-stream' });
152
+ }
153
+ export async function encryptBytes(bytes, options) {
154
+ const part = bytes instanceof ArrayBuffer ? bytes : asBlobPart(bytes);
155
+ const blob = new Blob([part]);
156
+ return encryptBlob({ blob, ...options });
157
+ }
158
+ export async function decryptToBytes(options) {
159
+ const blob = await decryptBlob(options);
160
+ return new Uint8Array(await blob.arrayBuffer());
161
+ }
162
+ //# sourceMappingURL=api.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.js","sourceRoot":"","sources":["../../../src/suites/aes-gcm-v2/api.ts"],"names":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AAO,CAAC,CAAA;IACvC,OAAO,IAAI,UAAU,CAAC,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC,CAAA;AACjD,CAAC"}
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Cipher suite 0x04: AES-256-GCM v2.
3
+ *
4
+ * Per-file content key. Per-chunk AEAD with file_id-bound AAD that
5
+ * authenticates chunk index, total chunks, and the is-final flag. The
6
+ * 12-byte GCM IV is split into an 8-byte file-derived prefix and a 4-byte
7
+ * big-endian counter — a wider prefix than v1 (0x01) so the per-file
8
+ * `(prefix, counter)` collision space across files sharing a content
9
+ * key shrinks from 2^32 to 2^64. The per-file chunk ceiling drops from
10
+ * 2^64 to 2^32 chunks, which is still well above any practical bound
11
+ * the format permits (`MAX_TOTAL_CHUNKS` is 1e9 and `chunkIndex` is a
12
+ * safe JS integer).
13
+ */
14
+ import { type ParsedHeader } from '../../internal/types.js';
15
+ export declare const AES_GCM_V2_TAG_BYTES = 16;
16
+ export declare const AES_GCM_V2_IV_BYTES = 12;
17
+ export declare const AES_GCM_V2_KEY_BYTES = 32;
18
+ export declare const AES_GCM_V2_NONCE_PREFIX_LENGTH = 8;
19
+ export declare const AES_GCM_V2_MAX_CHUNK_INDEX = 4294967295;
20
+ /** Suite payload layout: 60-byte wrapped key + 12-byte wrap IV = 72 bytes. */
21
+ export declare const AES_GCM_V2_SUITE_PAYLOAD_LENGTH = 72;
22
+ export interface AesGcmV2SuitePayload {
23
+ wrappedKey: Uint8Array;
24
+ wrapIv: Uint8Array;
25
+ }
26
+ export declare function buildAesGcmV2SuitePayload(payload: AesGcmV2SuitePayload): Uint8Array;
27
+ export declare function parseAesGcmV2SuitePayload(bytes: Uint8Array): AesGcmV2SuitePayload;
28
+ /** Generate a fresh per-file content key and file_id. */
29
+ export declare function generateContentMaterial(): {
30
+ contentKey: Uint8Array;
31
+ fileId: Uint8Array;
32
+ };
33
+ export interface AesGcmV2ChunkContext {
34
+ cryptoKey: CryptoKey;
35
+ noncePrefix: Uint8Array;
36
+ totalChunks: number;
37
+ }
38
+ export declare function createChunkContext(contentKey: Uint8Array, fileId: Uint8Array, totalChunks: number): Promise<AesGcmV2ChunkContext>;
39
+ export declare function encryptChunk(ctx: AesGcmV2ChunkContext, chunkIndex: number, plaintext: Uint8Array): Promise<Uint8Array>;
40
+ export declare function decryptChunk(ctx: AesGcmV2ChunkContext, chunkIndex: number, ciphertext: Uint8Array): Promise<Uint8Array>;
41
+ /**
42
+ * Wrap (encrypt) a content key under an envelope key (folder/vault key).
43
+ * The wrap layer is identical to v1: it is a one-shot AES-GCM encryption
44
+ * of a 32-byte key under a fresh 12-byte IV, which has no counter-mode
45
+ * collision concern.
46
+ */
47
+ export declare function wrapContentKey(options: {
48
+ contentKey: Uint8Array;
49
+ envelopeKey: Uint8Array;
50
+ envelopeAad?: Uint8Array;
51
+ }): Promise<{
52
+ wrapped: Uint8Array;
53
+ wrapIv: Uint8Array;
54
+ }>;
55
+ export declare function unwrapContentKey(options: {
56
+ envelopeKey: Uint8Array;
57
+ wrappedField: Uint8Array;
58
+ wrapIv: Uint8Array;
59
+ envelopeAad?: Uint8Array;
60
+ }): Promise<Uint8Array>;
61
+ /** Header parser hook — exposes ParsedHeader convenience for suite consumers. */
62
+ export declare function getSuitePayloadFromHeader(header: ParsedHeader): AesGcmV2SuitePayload;
63
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/suites/aes-gcm-v2/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,OAAO,EAGL,KAAK,YAAY,EAClB,MAAM,yBAAyB,CAAA;AAKhC,eAAO,MAAM,oBAAoB,KAAK,CAAA;AACtC,eAAO,MAAM,mBAAmB,KAAK,CAAA;AACrC,eAAO,MAAM,oBAAoB,KAAK,CAAA;AACtC,eAAO,MAAM,8BAA8B,IAAI,CAAA;AAC/C,eAAO,MAAM,0BAA0B,aAAc,CAAA;AAErD,8E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IACvB,YAAY,EAAE,UAAU,CAAA;IACxB,MAAM,EAAE,UAAU,CAAA;IAClB,WAAW,CAAC,EAAE,UAAU,CAAA;CACzB,GAAG,OAAO,CAAC,UAAU,CAAC,CA6BtB;AAED,iFAAiF;AACjF,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,YAAY,GACnB,oBAAoB,CAEtB"}
@@ -0,0 +1,193 @@
1
+ /**
2
+ * Cipher suite 0x04: AES-256-GCM v2.
3
+ *
4
+ * Per-file content key. Per-chunk AEAD with file_id-bound AAD that
5
+ * authenticates chunk index, total chunks, and the is-final flag. The
6
+ * 12-byte GCM IV is split into an 8-byte file-derived prefix and a 4-byte
7
+ * big-endian counter — a wider prefix than v1 (0x01) so the per-file
8
+ * `(prefix, counter)` collision space across files sharing a content
9
+ * key shrinks from 2^32 to 2^64. The per-file chunk ceiling drops from
10
+ * 2^64 to 2^32 chunks, which is still well above any practical bound
11
+ * the format permits (`MAX_TOTAL_CHUNKS` is 1e9 and `chunkIndex` is a
12
+ * safe JS integer).
13
+ */
14
+ import { buildChunkAad } from '../../format/header.js';
15
+ import { HKDF_INFO, HEADER_SIZES, } from '../../internal/types.js';
16
+ import { concatBytes, uint32BE } from '../../internal/encoding.js';
17
+ import { hkdfSha256 } from '../../internal/hkdf.js';
18
+ import { getSubtle, randomBytes } from '../../internal/runtime.js';
19
+ export const AES_GCM_V2_TAG_BYTES = 16;
20
+ export const AES_GCM_V2_IV_BYTES = 12;
21
+ export const AES_GCM_V2_KEY_BYTES = 32;
22
+ export const AES_GCM_V2_NONCE_PREFIX_LENGTH = 8;
23
+ export const AES_GCM_V2_MAX_CHUNK_INDEX = 0xffff_ffff;
24
+ /** Suite payload layout: 60-byte wrapped key + 12-byte wrap IV = 72 bytes. */
25
+ export const AES_GCM_V2_SUITE_PAYLOAD_LENGTH = 72;
26
+ export function buildAesGcmV2SuitePayload(payload) {
27
+ if (payload.wrappedKey.length !== 60) {
28
+ throw new RangeError('aes-gcm-v2: wrappedKey must be 60 bytes');
29
+ }
30
+ if (payload.wrapIv.length !== AES_GCM_V2_IV_BYTES) {
31
+ throw new RangeError('aes-gcm-v2: wrapIv must be 12 bytes');
32
+ }
33
+ return concatBytes([payload.wrappedKey, payload.wrapIv]);
34
+ }
35
+ export function parseAesGcmV2SuitePayload(bytes) {
36
+ if (bytes.length !== AES_GCM_V2_SUITE_PAYLOAD_LENGTH) {
37
+ throw new RangeError('aes-gcm-v2: suite payload must be 72 bytes');
38
+ }
39
+ return {
40
+ wrappedKey: bytes.slice(0, 60),
41
+ wrapIv: bytes.slice(60, 72),
42
+ };
43
+ }
44
+ /**
45
+ * Derive the per-file chunk key from the content key. The chunk-key HKDF
46
+ * info matches v1 because the chunk-key derivation is unchanged across
47
+ * versions; only the nonce-prefix derivation differs. Files of either
48
+ * suite encrypted with the same content_key + file_id derive identical
49
+ * chunk keys, but their `(prefix, counter)` spaces are disjoint because
50
+ * the prefix info string differs.
51
+ */
52
+ async function deriveChunkKey(contentKey, fileId) {
53
+ return hkdfSha256({
54
+ ikm: contentKey,
55
+ salt: fileId,
56
+ info: HKDF_INFO.AES_GCM_CHUNK_KEY,
57
+ length: AES_GCM_V2_KEY_BYTES,
58
+ });
59
+ }
60
+ /**
61
+ * Derive the 8-byte nonce prefix from file_id. Stable per file. The wider
62
+ * prefix is the entire point of the v2 suite — see this file's top
63
+ * docstring.
64
+ */
65
+ async function deriveNoncePrefix(fileId) {
66
+ return hkdfSha256({
67
+ ikm: fileId,
68
+ info: HKDF_INFO.AES_GCM_V2_NONCE_PREFIX,
69
+ length: AES_GCM_V2_NONCE_PREFIX_LENGTH,
70
+ });
71
+ }
72
+ function buildIv(noncePrefix, chunkIndex) {
73
+ if (!Number.isSafeInteger(chunkIndex) ||
74
+ chunkIndex < 0 ||
75
+ chunkIndex > AES_GCM_V2_MAX_CHUNK_INDEX) {
76
+ throw new RangeError('aes-gcm-v2: chunkIndex must be in [0, 2^32)');
77
+ }
78
+ if (noncePrefix.length !== AES_GCM_V2_NONCE_PREFIX_LENGTH) {
79
+ throw new RangeError('aes-gcm-v2: noncePrefix must be 8 bytes');
80
+ }
81
+ const iv = new Uint8Array(AES_GCM_V2_IV_BYTES);
82
+ iv.set(noncePrefix, 0);
83
+ iv.set(uint32BE(chunkIndex), AES_GCM_V2_NONCE_PREFIX_LENGTH);
84
+ return iv;
85
+ }
86
+ async function importAesGcmKey(keyBytes, usages) {
87
+ if (keyBytes.length !== AES_GCM_V2_KEY_BYTES) {
88
+ throw new RangeError('aes-gcm-v2: key must be 32 bytes');
89
+ }
90
+ return getSubtle().importKey('raw', keyBytes, { name: 'AES-GCM' }, false, usages);
91
+ }
92
+ /** Generate a fresh per-file content key and file_id. */
93
+ export function generateContentMaterial() {
94
+ return {
95
+ contentKey: randomBytes(AES_GCM_V2_KEY_BYTES),
96
+ fileId: randomBytes(HEADER_SIZES.FILE_ID),
97
+ };
98
+ }
99
+ export async function createChunkContext(contentKey, fileId, totalChunks) {
100
+ if (totalChunks > AES_GCM_V2_MAX_CHUNK_INDEX + 1) {
101
+ // Defense in depth — header-layer MAX_TOTAL_CHUNKS already caps this
102
+ // well below 2^32, but the suite enforces its own ceiling.
103
+ throw new RangeError('aes-gcm-v2: totalChunks exceeds 2^32 chunk-index space');
104
+ }
105
+ const chunkKeyBytes = await deriveChunkKey(contentKey, fileId);
106
+ const noncePrefix = await deriveNoncePrefix(fileId);
107
+ const cryptoKey = await importAesGcmKey(chunkKeyBytes, ['encrypt', 'decrypt']);
108
+ return { cryptoKey, noncePrefix, totalChunks };
109
+ }
110
+ export async function encryptChunk(ctx, chunkIndex, plaintext) {
111
+ const isFinal = chunkIndex === ctx.totalChunks - 1;
112
+ const aad = buildChunkAad(chunkIndex, ctx.totalChunks, isFinal);
113
+ const iv = buildIv(ctx.noncePrefix, chunkIndex);
114
+ const ct = await getSubtle().encrypt({
115
+ name: 'AES-GCM',
116
+ iv: iv,
117
+ additionalData: aad,
118
+ tagLength: 128,
119
+ }, ctx.cryptoKey, plaintext);
120
+ return new Uint8Array(ct);
121
+ }
122
+ export async function decryptChunk(ctx, chunkIndex, ciphertext) {
123
+ const isFinal = chunkIndex === ctx.totalChunks - 1;
124
+ const aad = buildChunkAad(chunkIndex, ctx.totalChunks, isFinal);
125
+ const iv = buildIv(ctx.noncePrefix, chunkIndex);
126
+ const pt = await getSubtle().decrypt({
127
+ name: 'AES-GCM',
128
+ iv: iv,
129
+ additionalData: aad,
130
+ tagLength: 128,
131
+ }, ctx.cryptoKey, ciphertext);
132
+ return new Uint8Array(pt);
133
+ }
134
+ /**
135
+ * Wrap (encrypt) a content key under an envelope key (folder/vault key).
136
+ * The wrap layer is identical to v1: it is a one-shot AES-GCM encryption
137
+ * of a 32-byte key under a fresh 12-byte IV, which has no counter-mode
138
+ * collision concern.
139
+ */
140
+ export async function wrapContentKey(options) {
141
+ const { contentKey, envelopeKey } = options;
142
+ if (contentKey.length !== AES_GCM_V2_KEY_BYTES) {
143
+ throw new RangeError('wrapContentKey: contentKey must be 32 bytes');
144
+ }
145
+ if (envelopeKey.length !== AES_GCM_V2_KEY_BYTES) {
146
+ throw new RangeError('wrapContentKey: envelopeKey must be 32 bytes');
147
+ }
148
+ const wrapIv = randomBytes(AES_GCM_V2_IV_BYTES);
149
+ const wrapKey = await importAesGcmKey(envelopeKey, ['encrypt']);
150
+ const wrapped = await getSubtle().encrypt({
151
+ name: 'AES-GCM',
152
+ iv: wrapIv,
153
+ additionalData: (options.envelopeAad ?? new Uint8Array(0)),
154
+ tagLength: 128,
155
+ }, wrapKey, contentKey);
156
+ const wrappedBytes = new Uint8Array(wrapped);
157
+ if (wrappedBytes.length !== 48) {
158
+ throw new Error('wrapContentKey: unexpected wrapped length');
159
+ }
160
+ const padded = new Uint8Array(60);
161
+ padded.set(wrappedBytes, 0);
162
+ return { wrapped: padded, wrapIv };
163
+ }
164
+ export async function unwrapContentKey(options) {
165
+ const { envelopeKey, wrappedField, wrapIv } = options;
166
+ if (envelopeKey.length !== AES_GCM_V2_KEY_BYTES) {
167
+ throw new RangeError('unwrapContentKey: envelopeKey must be 32 bytes');
168
+ }
169
+ if (wrappedField.length !== 60) {
170
+ throw new RangeError('unwrapContentKey: wrappedField must be 60 bytes');
171
+ }
172
+ if (wrapIv.length !== AES_GCM_V2_IV_BYTES) {
173
+ throw new RangeError('unwrapContentKey: wrapIv must be 12 bytes');
174
+ }
175
+ const wrapped = wrappedField.slice(0, 48);
176
+ const wrapKey = await importAesGcmKey(envelopeKey, ['decrypt']);
177
+ const pt = await getSubtle().decrypt({
178
+ name: 'AES-GCM',
179
+ iv: wrapIv,
180
+ additionalData: (options.envelopeAad ?? new Uint8Array(0)),
181
+ tagLength: 128,
182
+ }, wrapKey, wrapped);
183
+ const ptBytes = new Uint8Array(pt);
184
+ if (ptBytes.length !== AES_GCM_V2_KEY_BYTES) {
185
+ throw new Error('unwrapContentKey: bad plaintext length');
186
+ }
187
+ return ptBytes;
188
+ }
189
+ /** Header parser hook — exposes ParsedHeader convenience for suite consumers. */
190
+ export function getSuitePayloadFromHeader(header) {
191
+ return parseAesGcmV2SuitePayload(header.suitePayload);
192
+ }
193
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/suites/aes-gcm-v2/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;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,SAAS,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAA;AAElE,MAAM,CAAC,MAAM,oBAAoB,GAAG,EAAE,CAAA;AACtC,MAAM,CAAC,MAAM,mBAAmB,GAAG,EAAE,CAAA;AACrC,MA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kC,CACnC,CAAA;IACD,MAAM,OAAO,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAA;IAClC,IAAI,OAAO,CAAC,MAAM,KAAK,oBAAoB,EAAE,CAAC;QAC5C,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAA;IAC3D,CAAC;IACD,OAAO,OAAO,CAAA;AAChB,CAAC;AAED,iFAAiF;AACjF,MAAM,UAAU,yBAAyB,CACvC,MAAoB;IAEpB,OAAO,yBAAyB,CAAC,MAAM,CAAC,YAAY,CAAC,CAAA;AACvD,CAAC"}
@@ -0,0 +1,67 @@
1
+ /**
2
+ * High-level encrypt/decrypt API for the PQ-hybrid v1 suite.
3
+ *
4
+ * Differs from aes-gcm-v1 / xchacha-v1 because content key derivation is
5
+ * not a single random 32-byte buffer — instead, the combined key is
6
+ * recovered from the on-disk suite payload (PQ ciphertext + wrapped
7
+ * classical share) and the recipient's PQ secret key + envelope key.
8
+ *
9
+ * The header MAC is keyed by the *combined* key, so a successful header
10
+ * MAC verification proves both the PQ KEM and the classical wrap were
11
+ * recovered correctly.
12
+ */
13
+ import { type EncryptedFile, type ProgressCallback } from '../../internal/types.js';
14
+ export { decapsulateFromHeader, deriveMlKemKeypair, encapsulateForRecipient, generateMlKemKeypair, PQ_HYBRID_V1_SUITE_PAYLOAD_LENGTH, } from './index.js';
15
+ export interface PqHybridEncryptOptions {
16
+ blob: Blob;
17
+ /** Recipient's ML-KEM-1024 public key (1568 bytes) */
18
+ recipientPublicKey: Uint8Array;
19
+ /** Classical envelope key under which the classical share is wrapped */
20
+ envelopeKey: Uint8Array;
21
+ fileId?: Uint8Array;
22
+ chunkSize?: number;
23
+ onProgress?: ProgressCallback;
24
+ }
25
+ /**
26
+ * Encrypt a Blob under suite 0x03 (PQ-hybrid XChaCha20-Poly1305 +
27
+ * ML-KEM-1024). Returns the encrypted file alongside the combined
28
+ * content key used to produce it.
29
+ *
30
+ * SECURITY: The returned `combinedKey` grants permanent decryption
31
+ * capability for any file whose header was produced with it. Treat
32
+ * as cryptographic secret material: hold only in memory for the
33
+ * duration of the operation, do not persist to disk or storage, do
34
+ * not log, do not transmit. The only safe way to reproduce it on
35
+ * another machine is to re-encapsulate via the recipient's ML-KEM
36
+ * public key.
37
+ */
38
+ export declare function encryptBlob(options: PqHybridEncryptOptions): Promise<EncryptedFile & {
39
+ combinedKey: Uint8Array;
40
+ }>;
41
+ export interface PqHybridDecryptOptions {
42
+ blob: Blob;
43
+ /** Recipient's ML-KEM-1024 secret key (3168 bytes) */
44
+ recipientSecretKey: Uint8Array;
45
+ /** Classical envelope key */
46
+ envelopeKey: Uint8Array;
47
+ onProgress?: ProgressCallback;
48
+ }
49
+ export declare function decryptBlob(options: PqHybridDecryptOptions): Promise<Blob>;
50
+ /**
51
+ * Encrypt a Uint8Array under suite 0x03 (PQ-hybrid XChaCha20-Poly1305 +
52
+ * ML-KEM-1024). Returns the encrypted file alongside the combined
53
+ * content key used to produce it.
54
+ *
55
+ * SECURITY: The returned `combinedKey` grants permanent decryption
56
+ * capability for any file whose header was produced with it. Treat
57
+ * as cryptographic secret material: hold only in memory for the
58
+ * duration of the operation, do not persist to disk or storage, do
59
+ * not log, do not transmit. The only safe way to reproduce it on
60
+ * another machine is to re-encapsulate via the recipient's ML-KEM
61
+ * public key.
62
+ */
63
+ export declare function encryptBytes(bytes: Uint8Array, options: Omit<PqHybridEncryptOptions, 'blob'>): Promise<EncryptedFile & {
64
+ combinedKey: Uint8Array;
65
+ }>;
66
+ export declare function decryptToBytes(options: PqHybridDecryptOptions): Promise<Uint8Array>;
67
+ //# sourceMappingURL=api.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../../src/suites/pq-hybrid-v1/api.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAiBH,OAAO,EAEL,KAAK,aAAa,EAGlB,KAAK,gBAAgB,EAEtB,MAAM,yBAAyB,CAAA;AAchC,OAAO,EACL,qBAAqB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