@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,135 @@
1
+ /**
2
+ * Cast helper: TypeScript 5.7+ distinguishes Uint8Array<ArrayBuffer> from
3
+ * Uint8Array<ArrayBufferLike>. Blob and many Web Crypto APIs require the
4
+ * narrower variant. This helper centralizes the cast so we don't sprinkle
5
+ * `as Uint8Array<ArrayBuffer>` across the codebase.
6
+ *
7
+ * Functionally a no-op at runtime; purely a type annotation.
8
+ */
9
+ export function asBlobPart(bytes) {
10
+ return bytes;
11
+ }
12
+ /**
13
+ * Byte-level encoding helpers.
14
+ *
15
+ * All multi-byte integer functions are explicitly big-endian to match the
16
+ * v1 wire format. Callers should prefer these over hand-rolled bit-shifting.
17
+ */
18
+ export function bytesToBase64(bytes) {
19
+ if (typeof btoa === 'function') {
20
+ let binary = '';
21
+ for (let i = 0; i < bytes.length; i += 1) {
22
+ binary += String.fromCharCode(bytes[i]);
23
+ }
24
+ return btoa(binary);
25
+ }
26
+ // Node fallback. Uses Buffer; safe in any Node version.
27
+ return Buffer.from(bytes).toString('base64');
28
+ }
29
+ export function base64ToBytes(value) {
30
+ if (typeof value !== 'string') {
31
+ throw new TypeError('base64ToBytes: input must be a string');
32
+ }
33
+ if (typeof atob === 'function') {
34
+ const binary = atob(value);
35
+ const out = new Uint8Array(binary.length);
36
+ for (let i = 0; i < binary.length; i += 1) {
37
+ out[i] = binary.charCodeAt(i);
38
+ }
39
+ return out;
40
+ }
41
+ return new Uint8Array(Buffer.from(value, 'base64'));
42
+ }
43
+ export function bytesToHex(bytes) {
44
+ let out = '';
45
+ for (let i = 0; i < bytes.length; i += 1) {
46
+ out += bytes[i].toString(16).padStart(2, '0');
47
+ }
48
+ return out;
49
+ }
50
+ const HEX_RE = /^[0-9a-f]*$/i;
51
+ export function hexToBytes(hex) {
52
+ if (typeof hex !== 'string') {
53
+ throw new TypeError('hexToBytes: input must be a string');
54
+ }
55
+ if (hex.length % 2 !== 0 || !HEX_RE.test(hex)) {
56
+ throw new TypeError('hexToBytes: input must be even-length hexadecimal');
57
+ }
58
+ const out = new Uint8Array(hex.length / 2);
59
+ for (let i = 0; i < out.length; i += 1) {
60
+ out[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16);
61
+ }
62
+ return out;
63
+ }
64
+ export function concatBytes(parts) {
65
+ let total = 0;
66
+ for (const part of parts)
67
+ total += part.length;
68
+ const out = new Uint8Array(total);
69
+ let offset = 0;
70
+ for (const part of parts) {
71
+ out.set(part, offset);
72
+ offset += part.length;
73
+ }
74
+ return out;
75
+ }
76
+ export function uint32BE(value) {
77
+ if (!Number.isSafeInteger(value) || value < 0 || value > 0xffff_ffff) {
78
+ throw new RangeError('uint32BE: value out of range');
79
+ }
80
+ const out = new Uint8Array(4);
81
+ new DataView(out.buffer).setUint32(0, value, false);
82
+ return out;
83
+ }
84
+ export function readUint32BE(bytes, offset = 0) {
85
+ if (offset + 4 > bytes.length) {
86
+ throw new RangeError('readUint32BE: insufficient bytes');
87
+ }
88
+ return new DataView(bytes.buffer, bytes.byteOffset + offset, 4).getUint32(0, false);
89
+ }
90
+ export function uint16BE(value) {
91
+ if (!Number.isSafeInteger(value) || value < 0 || value > 0xffff) {
92
+ throw new RangeError('uint16BE: value out of range');
93
+ }
94
+ const out = new Uint8Array(2);
95
+ new DataView(out.buffer).setUint16(0, value, false);
96
+ return out;
97
+ }
98
+ export function readUint16BE(bytes, offset = 0) {
99
+ if (offset + 2 > bytes.length) {
100
+ throw new RangeError('readUint16BE: insufficient bytes');
101
+ }
102
+ return new DataView(bytes.buffer, bytes.byteOffset + offset, 2).getUint16(0, false);
103
+ }
104
+ /**
105
+ * Encode a non-negative integer as 8 big-endian bytes. The library never
106
+ * emits values exceeding Number.MAX_SAFE_INTEGER (2^53 - 1) because the
107
+ * format spec caps total_chunks at 1e9 and plaintext_size is bounded by
108
+ * what JavaScript can sensibly handle.
109
+ */
110
+ export function uint64BE(value) {
111
+ if (!Number.isSafeInteger(value) || value < 0) {
112
+ throw new RangeError('uint64BE: value must be a non-negative safe integer');
113
+ }
114
+ const out = new Uint8Array(8);
115
+ // Top 4 bytes: high uint32 (value / 2^32, truncated)
116
+ const high = Math.floor(value / 0x1_0000_0000);
117
+ const low = value >>> 0;
118
+ new DataView(out.buffer).setUint32(0, high, false);
119
+ new DataView(out.buffer).setUint32(4, low, false);
120
+ return out;
121
+ }
122
+ export function readUint64BE(bytes, offset = 0) {
123
+ if (offset + 8 > bytes.length) {
124
+ throw new RangeError('readUint64BE: insufficient bytes');
125
+ }
126
+ const view = new DataView(bytes.buffer, bytes.byteOffset + offset, 8);
127
+ const high = view.getUint32(0, false);
128
+ const low = view.getUint32(4, false);
129
+ // Reject values that don't fit in a safe JS integer (> 2^53 - 1).
130
+ if (high > 0x1f_ffff) {
131
+ throw new RangeError('readUint64BE: value exceeds Number.MAX_SAFE_INTEGER');
132
+ }
133
+ return high * 0x1_0000_0000 + low;
134
+ }
135
+ //# sourceMappingURL=encoding.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encoding.js","sourceRoot":"","sources":["../../src/internal/encoding.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,UAAU,UAAU,CAAC,KAAiB;IAC1C,OAAO,KAAgC,CAAA;AACzC,CAAC;AAED;;;;;GAKG;AAEH,MAAM,UAAU,aAAa,CAAC,KAAiB;IAC7C,IAAI,OAAO,IAAI,KAAK,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AAM,GAAG,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;QAC9B,MAAM,IAAI,UAAU,CAAC,kCAAkC,CAAC,CAAA;IAC1D,CAAC;IACD,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,UAAU,GAAG,MAAM,EAAE,CAAC,CAAC,CAAA;IACrE,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,KAAK,CAAC,CAAA;IACrC,MAAM,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,KAAK,CAAC,CAAA;IACpC,kEAAkE;IAClE,IAAI,IAAI,GAAG,SAAS,EAAE,CAAC;QACrB,MAAM,IAAI,UAAU,CAAC,qDAAqD,CAAC,CAAA;IAC7E,CAAC;IACD,OAAO,IAAI,GAAG,aAAa,GAAG,GAAG,CAAA;AACnC,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * HKDF-SHA-256 wrapper.
3
+ *
4
+ * Every domain-separated key the library derives from another key flows
5
+ * through this function. Domain strings are constants in `internal/types.ts`
6
+ * — never construct an `info` parameter ad-hoc at the call site.
7
+ */
8
+ export declare function hkdfSha256(options: {
9
+ ikm: Uint8Array;
10
+ salt?: Uint8Array;
11
+ info: string;
12
+ length: number;
13
+ }): Promise<Uint8Array>;
14
+ //# sourceMappingURL=hkdf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hkdf.d.ts","sourceRoot":"","sources":["../../src/internal/hkdf.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAMH,wBAAsB,UAAU,CAAC,OAAO,EAAE;IACxC,GAAG,EAAE,UAAU,CAAA;IACf,IAAI,CAAC,EAAE,UAAU,CAAA;IACjB,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,MAAM,CAAA;CACf,GAAG,OAAO,CAAC,UAAU,CAAC,CA6BtB"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * HKDF-SHA-256 wrapper.
3
+ *
4
+ * Every domain-separated key the library derives from another key flows
5
+ * through this function. Domain strings are constants in `internal/types.ts`
6
+ * — never construct an `info` parameter ad-hoc at the call site.
7
+ */
8
+ import { getSubtle } from './runtime.js';
9
+ const TEXT_ENCODER = new TextEncoder();
10
+ export async function hkdfSha256(options) {
11
+ const { ikm, info, length } = options;
12
+ const salt = options.salt ?? new Uint8Array(32); // SHA-256 zero-salt fallback
13
+ if (!Number.isSafeInteger(length) || length <= 0 || length > 8160) {
14
+ // 8160 = 255 * 32, the HKDF-SHA-256 output bound.
15
+ throw new RangeError('hkdfSha256: length out of range (1..8160)');
16
+ }
17
+ const subtle = getSubtle();
18
+ const baseKey = await subtle.importKey('raw', ikm, 'HKDF', false, ['deriveBits']);
19
+ const bits = await subtle.deriveBits({
20
+ name: 'HKDF',
21
+ hash: 'SHA-256',
22
+ salt: salt,
23
+ info: TEXT_ENCODER.encode(info),
24
+ }, baseKey, length * 8);
25
+ return new Uint8Array(bits);
26
+ }
27
+ //# sourceMappingURL=hkdf.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hkdf.js","sourceRoot":"","sources":["../../src/internal/hkdf.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAExC,MAAM,YAAY,GAAG,IAAI,WAAW,EAAE,CAAA;AAEtC,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,OAKhC;IACC,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAA;IACrC,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,IAAI,UAAU,CAAC,EAAE,CAAC,CAAA,CAAC,6BAA6B;IAC7E,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,CAAC,IAAI,MAAM,GAAG,IAAI,EAAE,CAAC;QAClE,kDAAkD;QAClD,MAAM,IAAI,UAAU,CAAC,2CAA2C,CAAC,CAAA;IACnE,CAAC;IAED,MAAM,MAAM,GAAG,SAAS,EAAE,CAAA;IAC1B,MAAM,OAAO,GAAG,MAAM,MAAM,CAAC,SAAS,CACpC,KAAK,EACL,GAA8B,EAC9B,MAAM,EACN,KAAK,EACL,CAAC,YAAY,CAAC,CACf,CAAA;IAED,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,UAAU,CAClC;QACE,IAAI,EAAE,MAAM;QACZ,IAAI,EAAE,SAAS;QACf,IAAI,EAAE,IAA+B;QACrC,IAAI,EAAE,YAAY,CAAC,MAAM,CAAC,IAAI,CAA4B;KAC3D,EACD,OAAO,EACP,MAAM,GAAG,CAAC,CACX,CAAA;IAED,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC,CAAA;AAC7B,CAAC"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * HMAC-SHA-256 wrapper.
3
+ *
4
+ * Used for the file-format header MAC and for any application-level
5
+ * authenticator the library exposes. Always verify with constantTimeEqual.
6
+ */
7
+ export declare function hmacSha256(key: Uint8Array, data: Uint8Array): Promise<Uint8Array>;
8
+ export declare function hmacSha256Verify(key: Uint8Array, data: Uint8Array, expected: Uint8Array): Promise<boolean>;
9
+ //# sourceMappingURL=hmac.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hmac.d.ts","sourceRoot":"","sources":["../../src/internal/hmac.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,wBAAsB,UAAU,CAC9B,GAAG,EAAE,UAAU,EACf,IAAI,EAAE,UAAU,GACf,OAAO,CAAC,UAAU,CAAC,CAWrB;AAED,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,UAAU,EACf,IAAI,EAAE,UAAU,EAChB,QAAQ,EAAE,UAAU,GACnB,OAAO,CAAC,OAAO,CAAC,CAGlB"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * HMAC-SHA-256 wrapper.
3
+ *
4
+ * Used for the file-format header MAC and for any application-level
5
+ * authenticator the library exposes. Always verify with constantTimeEqual.
6
+ */
7
+ import { constantTimeEqual, getSubtle } from './runtime.js';
8
+ export async function hmacSha256(key, data) {
9
+ const subtle = getSubtle();
10
+ const hmacKey = await subtle.importKey('raw', key, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
11
+ const sig = await subtle.sign('HMAC', hmacKey, data);
12
+ return new Uint8Array(sig);
13
+ }
14
+ export async function hmacSha256Verify(key, data, expected) {
15
+ const computed = await hmacSha256(key, data);
16
+ return constantTimeEqual(computed, expected);
17
+ }
18
+ //# sourceMappingURL=hmac.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hmac.js","sourceRoot":"","sources":["../../src/internal/hmac.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAE3D,MAAM,CAAC,KAAK,UAAU,UAAU,CAC9B,GAAe,EACf,IAAgB;IAEhB,MAAM,MAAM,GAAG,SAAS,EAAE,CAAA;IAC1B,MAAM,OAAO,GAAG,MAAM,MAAM,CAAC,SAAS,CACpC,KAAK,EACL,GAA8B,EAC9B,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,EACjC,KAAK,EACL,CAAC,MAAM,CAAC,CACT,CAAA;IACD,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,OAAO,EAAE,IAA+B,CAAC,CAAA;IAC/E,OAAO,IAAI,UAAU,CAAC,GAAG,CAAC,CAAA;AAC5B,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,GAAe,EACf,IAAgB,EAChB,QAAoB;IAEpB,MAAM,QAAQ,GAAG,MAAM,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,CAAA;IAC5C,OAAO,iBAAiB,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAA;AAC9C,CAAC"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Runtime detection and validation for the host environment.
3
+ *
4
+ * This module is intentionally tiny. The library targets any environment
5
+ * exposing the WHATWG Web Crypto API at `globalThis.crypto.subtle` —
6
+ * modern browsers, Node 20+, Deno, Bun, Cloudflare Workers, and any
7
+ * compatible WebAssembly runtime.
8
+ *
9
+ * Suites that require additional primitives (Argon2id, ML-KEM,
10
+ * XChaCha20-Poly1305) load those primitives lazily from
11
+ * `libsodium-wrappers-sumo` and `@noble/post-quantum`. Consumers that
12
+ * only use the AES-GCM suite never load those WASM blobs.
13
+ */
14
+ import type { Crypto, SubtleCrypto } from './types.js';
15
+ export declare function getCrypto(): Crypto;
16
+ export declare function getSubtle(): SubtleCrypto;
17
+ export declare function randomBytes(length: number): Uint8Array;
18
+ /**
19
+ * Constant-time equality for byte arrays. Used wherever a comparison failure
20
+ * could leak timing information (MAC verification, key comparison).
21
+ */
22
+ export declare function constantTimeEqual(a: Uint8Array | null | undefined, b: Uint8Array | null | undefined): boolean;
23
+ /**
24
+ * Best-effort zeroization of secret material. JavaScript engines may have
25
+ * already copied or reallocated the underlying buffer; this is a hint, not
26
+ * a guarantee. Use only for in-process hardening.
27
+ */
28
+ export declare function zeroize(buffer: Uint8Array): void;
29
+ //# sourceMappingURL=runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/internal/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAItD,wBAAgB,SAAS,IAAI,MAAM,CAkBlC;AAED,wBAAgB,SAAS,IAAI,YAAY,CAExC;AAED,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAWtD;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,CAAC,EAAE,UAAU,GAAG,IAAI,GAAG,SAAS,EAChC,CAAC,EAAE,UAAU,GAAG,IAAI,GAAG,SAAS,GAC/B,OAAO,CAQT;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,UAAU,GAAG,IAAI,CAEhD"}
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Runtime detection and validation for the host environment.
3
+ *
4
+ * This module is intentionally tiny. The library targets any environment
5
+ * exposing the WHATWG Web Crypto API at `globalThis.crypto.subtle` —
6
+ * modern browsers, Node 20+, Deno, Bun, Cloudflare Workers, and any
7
+ * compatible WebAssembly runtime.
8
+ *
9
+ * Suites that require additional primitives (Argon2id, ML-KEM,
10
+ * XChaCha20-Poly1305) load those primitives lazily from
11
+ * `libsodium-wrappers-sumo` and `@noble/post-quantum`. Consumers that
12
+ * only use the AES-GCM suite never load those WASM blobs.
13
+ */
14
+ let cachedCrypto = null;
15
+ export function getCrypto() {
16
+ if (cachedCrypto)
17
+ return cachedCrypto;
18
+ const candidate = globalThis.crypto;
19
+ if (!candidate ||
20
+ !candidate.subtle ||
21
+ typeof candidate.getRandomValues !== 'function') {
22
+ throw new Error('shieldfive-crypto: Web Crypto API is not available. ' +
23
+ 'This library requires globalThis.crypto with SubtleCrypto support. ' +
24
+ 'In Node, use Node 20 or later.');
25
+ }
26
+ cachedCrypto = candidate;
27
+ return candidate;
28
+ }
29
+ export function getSubtle() {
30
+ return getCrypto().subtle;
31
+ }
32
+ export function randomBytes(length) {
33
+ if (!Number.isSafeInteger(length) || length <= 0) {
34
+ throw new RangeError('randomBytes: length must be a positive integer');
35
+ }
36
+ if (length > 65536) {
37
+ // Web Crypto specification limits getRandomValues to 65536 bytes per call.
38
+ throw new RangeError('randomBytes: length exceeds 65536');
39
+ }
40
+ const out = new Uint8Array(length);
41
+ getCrypto().getRandomValues(out);
42
+ return out;
43
+ }
44
+ /**
45
+ * Constant-time equality for byte arrays. Used wherever a comparison failure
46
+ * could leak timing information (MAC verification, key comparison).
47
+ */
48
+ export function constantTimeEqual(a, b) {
49
+ if (!a || !b)
50
+ return false;
51
+ if (a.length !== b.length)
52
+ return false;
53
+ let diff = 0;
54
+ for (let i = 0; i < a.length; i += 1) {
55
+ diff |= a[i] ^ b[i];
56
+ }
57
+ return diff === 0;
58
+ }
59
+ /**
60
+ * Best-effort zeroization of secret material. JavaScript engines may have
61
+ * already copied or reallocated the underlying buffer; this is a hint, not
62
+ * a guarantee. Use only for in-process hardening.
63
+ */
64
+ export function zeroize(buffer) {
65
+ buffer.fill(0);
66
+ }
67
+ //# sourceMappingURL=runtime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../../src/internal/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAIH,IAAI,YAAY,GAAkB,IAAI,CAAA;AAEtC,MAAM,UAAU,SAAS;IACvB,IAAI,YAAY;QAAE,OAAO,YAAY,CAAA;IAErC,MAAM,SAAS,GAAI,UAAkC,CAAC,MAAM,CAAA;IAC5D,IACE,CAAC,SAAS;QACV,CAAC,SAAS,CAAC,MAAM;QACjB,OAAO,SAAS,CAAC,eAAe,KAAK,UAAU,EAC/C,CAAC;QACD,MAAM,IAAI,KAAK,CACb,sDAAsD;YACpD,qEAAqE;YACrE,gCAAgC,CACnC,CAAA;IACH,CAAC;IAED,YAAY,GAAG,SAAS,CAAA;IACxB,OAAO,SAAS,CAAA;AAClB,CAAC;AAED,MAAM,UAAU,SAAS;IACvB,OAAO,SAAS,EAAE,CAAC,MAAM,CAAA;AAC3B,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,MAAc;IACxC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,CAAC,EAAE,CAAC;QACjD,MAAM,IAAI,UAAU,CAAC,gDAAgD,CAAC,CAAA;IACxE,CAAC;IACD,IAAI,MAAM,GAAG,KAAK,EAAE,CAAC;QACnB,2EAA2E;QAC3E,MAAM,IAAI,UAAU,CAAC,mCAAmC,CAAC,CAAA;IAC3D,CAAC;IACD,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAA;IAClC,SAAS,EAAE,CAAC,eAAe,CAAC,GAAG,CAAC,CAAA;IAChC,OAAO,GAAG,CAAA;AACZ,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAC/B,CAAgC,EAChC,CAAgC;IAEhC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC;QAAE,OAAO,KAAK,CAAA;IAC1B,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QAAE,OAAO,KAAK,CAAA;IACvC,IAAI,IAAI,GAAG,CAAC,CAAA;IACZ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QACrC,IAAI,IAAI,CAAC,CAAC,CAAC,CAAE,GAAG,CAAC,CAAC,CAAC,CAAE,CAAA;IACvB,CAAC;IACD,OAAO,IAAI,KAAK,CAAC,CAAA;AACnB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,OAAO,CAAC,MAAkB;IACxC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;AAChB,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Lazy loader for libsodium-wrappers-sumo.
3
+ *
4
+ * libsodium is loaded on demand only when a suite that needs it (XChaCha,
5
+ * PQ hybrid, password-based KDF) is actually used. Consumers using only
6
+ * AES-256-GCM never load the WASM blob.
7
+ *
8
+ * libsodium is declared as an optional peer dependency in package.json.
9
+ * Bundlers should treat it as side-effect-free.
10
+ */
11
+ export declare function getSodium(): Promise<any>;
12
+ //# sourceMappingURL=sodium.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sodium.d.ts","sourceRoot":"","sources":["../../src/internal/sodium.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAKH,wBAAsB,SAAS,IAAI,OAAO,CAAC,GAAG,CAAC,CAuB9C"}
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Lazy loader for libsodium-wrappers-sumo.
3
+ *
4
+ * libsodium is loaded on demand only when a suite that needs it (XChaCha,
5
+ * PQ hybrid, password-based KDF) is actually used. Consumers using only
6
+ * AES-256-GCM never load the WASM blob.
7
+ *
8
+ * libsodium is declared as an optional peer dependency in package.json.
9
+ * Bundlers should treat it as side-effect-free.
10
+ */
11
+ let cachedSodium = null;
12
+ let pendingInit = null;
13
+ export async function getSodium() {
14
+ if (cachedSodium)
15
+ return cachedSodium;
16
+ if (!pendingInit) {
17
+ pendingInit = (async () => {
18
+ let sodium;
19
+ try {
20
+ // Dynamic import keeps libsodium out of bundles that don't use it.
21
+ sodium = (await import('libsodium-wrappers-sumo')).default;
22
+ }
23
+ catch (err) {
24
+ throw new Error('shieldfive-crypto: libsodium-wrappers-sumo is required for ' +
25
+ 'this cipher suite. Install it as a dependency.', { cause: err });
26
+ }
27
+ await sodium.ready;
28
+ cachedSodium = sodium;
29
+ return sodium;
30
+ })();
31
+ }
32
+ return pendingInit;
33
+ }
34
+ //# sourceMappingURL=sodium.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sodium.js","sourceRoot":"","sources":["../../src/internal/sodium.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,IAAI,YAAY,GAAe,IAAI,CAAA;AACnC,IAAI,WAAW,GAAwB,IAAI,CAAA;AAE3C,MAAM,CAAC,KAAK,UAAU,SAAS;IAC7B,IAAI,YAAY;QAAE,OAAO,YAAY,CAAA;IAErC,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,WAAW,GAAG,CAAC,KAAK,IAAI,EAAE;YACxB,IAAI,MAAW,CAAA;YACf,IAAI,CAAC;gBACH,mEAAmE;gBACnE,MAAM,GAAG,CAAC,MAAM,MAAM,CAAC,yBAAyB,CAAC,CAAC,CAAC,OAAO,CAAA;YAC5D,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,MAAM,IAAI,KAAK,CACb,6DAA6D;oBAC3D,gDAAgD,EAClD,EAAE,KAAK,EAAE,GAAY,EAAE,CACxB,CAAA;YACH,CAAC;YACD,MAAM,MAAM,CAAC,KAAK,CAAA;YAClB,YAAY,GAAG,MAAM,CAAA;YACrB,OAAO,MAAM,CAAA;QACf,CAAC,CAAC,EAAE,CAAA;IACN,CAAC;IAED,OAAO,WAAW,CAAA;AACpB,CAAC"}
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Public type definitions for the shieldfive-crypto library.
3
+ */
4
+ export type Crypto = {
5
+ subtle: SubtleCrypto;
6
+ getRandomValues<T extends ArrayBufferView>(array: T): T;
7
+ };
8
+ export type SubtleCrypto = globalThis.SubtleCrypto;
9
+ /**
10
+ * Suite identifiers as written to the on-disk format byte.
11
+ * See `spec/format-v1.md` for the canonical assignment.
12
+ */
13
+ export declare const SUITE: {
14
+ readonly AES_256_GCM_V1: 1;
15
+ readonly XCHACHA20_POLY1305_V1: 2;
16
+ readonly PQ_HYBRID_XCHACHA_MLKEM1024_V1: 3;
17
+ readonly AES_256_GCM_V2: 4;
18
+ };
19
+ export type SuiteId = (typeof SUITE)[keyof typeof SUITE];
20
+ export type SuiteName = 'aes-256-gcm-v1' | 'xchacha20-poly1305-v1' | 'pq-hybrid-xchacha-mlkem1024-v1' | 'aes-256-gcm-v2';
21
+ export declare const SUITE_NAMES: Record<SuiteId, SuiteName>;
22
+ /**
23
+ * Magic bytes prefixing every v1-format file: "SF5" + major(1) + minor(0).
24
+ * Treat as immutable; do not mutate.
25
+ */
26
+ export declare const FORMAT_MAGIC: Uint8Array;
27
+ export declare const FORMAT_MAGIC_LENGTH = 5;
28
+ /** Header field sizes — see spec/format-v1.md */
29
+ export declare const HEADER_SIZES: Readonly<{
30
+ MAGIC: 5;
31
+ SUITE: 1;
32
+ FLAGS: 1;
33
+ FILE_ID: 16;
34
+ CHUNK_SIZE: 4;
35
+ TOTAL_CHUNKS: 8;
36
+ PLAINTEXT_SIZE: 8;
37
+ SUITE_PAYLOAD_LEN: 2;
38
+ HEADER_MAC: 32;
39
+ }>;
40
+ /** Fixed bytes preceding the variable-length suite_payload */
41
+ export declare const HEADER_FIXED_PREFIX_LENGTH: number;
42
+ /** Default plaintext bytes per chunk for new uploads (4 MiB) */
43
+ export declare const DEFAULT_CHUNK_SIZE: number;
44
+ /** Maximum permitted chunk_size in the format (uint32 max) */
45
+ export declare const MAX_CHUNK_SIZE = 4294967295;
46
+ /** Maximum permitted total_chunks (sane upper bound, well below uint64 max) */
47
+ export declare const MAX_TOTAL_CHUNKS = 1000000000;
48
+ /** Domain separators for HKDF — never reuse a string here in another context */
49
+ export declare const HKDF_INFO: Readonly<{
50
+ HEADER_MAC: "shieldfive/v1/header-mac";
51
+ AES_GCM_CHUNK_KEY: "shieldfive/v1/aes-gcm/chunk-key";
52
+ AES_GCM_NONCE_PREFIX: "shieldfive/v1/aes-gcm/nonce-prefix";
53
+ AES_GCM_V2_NONCE_PREFIX: "shieldfive/v1/aes-gcm-v2/nonce-prefix";
54
+ XCHACHA_CHUNK_KEY: "shieldfive/v1/xchacha/chunk-key";
55
+ XCHACHA_NONCE_PREFIX: "shieldfive/v1/xchacha/nonce-prefix";
56
+ PQ_HYBRID_COMBINE: "shieldfive/v1/pq-hybrid/combine";
57
+ ML_KEM_1024_SEED: "shieldfive/v1/pq-hybrid/ml-kem-1024-seed";
58
+ ARGON2ID_SALT_COMPRESSION: "shieldfive/v1/argon2id/salt-compression";
59
+ }>;
60
+ /** AAD domain string mixed into every chunk's authenticator */
61
+ export declare const CHUNK_AAD_DOMAIN = "shieldfive/v1/chunk";
62
+ export declare const CHUNK_AAD_LENGTH: number;
63
+ /**
64
+ * Header values reflecting how a particular file was encrypted.
65
+ * Returned by header parsers; consumed by chunk encrypt/decrypt loops.
66
+ */
67
+ export interface ParsedHeader {
68
+ suite: SuiteId;
69
+ flags: number;
70
+ fileId: Uint8Array;
71
+ chunkSize: number;
72
+ totalChunks: number;
73
+ plaintextSize: number;
74
+ suitePayload: Uint8Array;
75
+ /** Bytes consumed from the input — chunks start at this offset */
76
+ headerLength: number;
77
+ }
78
+ /**
79
+ * Result of encrypting a file. The blob is the on-disk form (header +
80
+ * concatenated chunks). Application metadata (file id, suite, chunk size)
81
+ * is also returned so callers can stash it in their own database if they
82
+ * want to look files up by id.
83
+ */
84
+ export interface EncryptedFile {
85
+ blob: Blob;
86
+ suite: SuiteId;
87
+ fileId: Uint8Array;
88
+ chunkSize: number;
89
+ totalChunks: number;
90
+ plaintextSize: number;
91
+ }
92
+ /**
93
+ * Progress callback shape, called after each chunk is processed. Progress
94
+ * is reported as a float in [0, 1] inclusive. The final call is always
95
+ * exactly 1.0.
96
+ */
97
+ export type ProgressCallback = (progress: number) => void;
98
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/internal/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,MAAM,MAAM,GAAG;IACnB,MAAM,EAAE,YAAY,CAAA;IACpB,eAAe,CAAC,CAAC,SAAS,eAAe,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAAA;CACxD,CAAA;AAED,MAAM,MAAM,YAAY,GAAG,UAAU,CAAC,YAAY,CAAA;AAElD;;;GAGG;AACH,eAAO,MAAM,KAAK;;;;;CAKR,CAAA;AAEV,MAAM,MAAM,OAAO,GAAG,CAAC,OAAO,KAAK,CAAC,CAAC,MAAM,OAAO,KAAK,CAAC,CAAA;AAExD,MAAM,MAAM,SAAS,GACjB,gBAAgB,GAChB,uBAAuB,GACvB,gCAAgC,GAChC,gBAAgB,CAAA;AAEpB,eAAO,MAAM,WAAW,EAAE,MAAM,CAAC,OAAO,EAAE,SAAS,CAKlD,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,YAAY,EAAE,UAEzB,CAAA;AAEF,eAAO,MAAM,mBAAmB,IAAI,CAAA;AAEpC,iDAAiD;AACjD,eAAO,MAAM,YAAY;;;;;;;;;;EAUvB,CAAA;AAEF,8DAA8D;AAC9D,eAAO,MAAM,0BAA0B,QAQP,CAAA;AAEhC,gEAAgE;AAChE,eAAO,MAAM,kBAAkB,QAAkB,CAAA;AAEjD,8DAA8D;AAC9D,eAAO,MAAM,cAAc,aAAc,CAAA;AAEzC,+EAA+E;AAC/E,eAAO,MAAM,gBAAgB,aAAgB,CAAA;AAE7C,gFAAgF;AAChF,eAAO,MAAM,SAAS;;;;;;;;;;EAUpB,CAAA;AAEF,+DAA+D;AAC/D,eAAO,MAAM,gBAAgB,wBAAwB,CAAA;AACrD,eAAO,MAAM,gBAAgB,QAAsC,CAAA;AAEnE;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,OAAO,CAAA;IACd,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,UAAU,CAAA;IAClB,SAAS,EAAE,MAAM,CAAA;IACjB,WAAW,EAAE,MAAM,CAAA;IACnB,aAAa,EAAE,MAAM,CAAA;IACrB,YAAY,EAAE,UAAU,CAAA;IACxB,kEAAkE;IAClE,YAAY,EAAE,MAAM,CAAA;CACrB;AAED;;;;;GAKG;AACH,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,IAAI,CAAA;IACV,KAAK,EAAE,OAAO,CAAA;IACd,MAAM,EAAE,UAAU,CAAA;IAClB,SAAS,EAAE,MAAM,CAAA;IACjB,WAAW,EAAE,MAAM,CAAA;IACnB,aAAa,EAAE,MAAM,CAAA;CACtB;AAED;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAA"}
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Public type definitions for the shieldfive-crypto library.
3
+ */
4
+ /**
5
+ * Suite identifiers as written to the on-disk format byte.
6
+ * See `spec/format-v1.md` for the canonical assignment.
7
+ */
8
+ export const SUITE = {
9
+ AES_256_GCM_V1: 0x01,
10
+ XCHACHA20_POLY1305_V1: 0x02,
11
+ PQ_HYBRID_XCHACHA_MLKEM1024_V1: 0x03,
12
+ AES_256_GCM_V2: 0x04,
13
+ };
14
+ export const SUITE_NAMES = {
15
+ [SUITE.AES_256_GCM_V1]: 'aes-256-gcm-v1',
16
+ [SUITE.XCHACHA20_POLY1305_V1]: 'xchacha20-poly1305-v1',
17
+ [SUITE.PQ_HYBRID_XCHACHA_MLKEM1024_V1]: 'pq-hybrid-xchacha-mlkem1024-v1',
18
+ [SUITE.AES_256_GCM_V2]: 'aes-256-gcm-v2',
19
+ };
20
+ /**
21
+ * Magic bytes prefixing every v1-format file: "SF5" + major(1) + minor(0).
22
+ * Treat as immutable; do not mutate.
23
+ */
24
+ export const FORMAT_MAGIC = new Uint8Array([
25
+ 0x53, 0x46, 0x35, 0x01, 0x00,
26
+ ]);
27
+ export const FORMAT_MAGIC_LENGTH = 5;
28
+ /** Header field sizes — see spec/format-v1.md */
29
+ export const HEADER_SIZES = Object.freeze({
30
+ MAGIC: 5,
31
+ SUITE: 1,
32
+ FLAGS: 1,
33
+ FILE_ID: 16,
34
+ CHUNK_SIZE: 4,
35
+ TOTAL_CHUNKS: 8,
36
+ PLAINTEXT_SIZE: 8,
37
+ SUITE_PAYLOAD_LEN: 2,
38
+ HEADER_MAC: 32,
39
+ });
40
+ /** Fixed bytes preceding the variable-length suite_payload */
41
+ export const HEADER_FIXED_PREFIX_LENGTH = HEADER_SIZES.MAGIC +
42
+ HEADER_SIZES.SUITE +
43
+ HEADER_SIZES.FLAGS +
44
+ HEADER_SIZES.FILE_ID +
45
+ HEADER_SIZES.CHUNK_SIZE +
46
+ HEADER_SIZES.TOTAL_CHUNKS +
47
+ HEADER_SIZES.PLAINTEXT_SIZE +
48
+ HEADER_SIZES.SUITE_PAYLOAD_LEN;
49
+ /** Default plaintext bytes per chunk for new uploads (4 MiB) */
50
+ export const DEFAULT_CHUNK_SIZE = 4 * 1024 * 1024;
51
+ /** Maximum permitted chunk_size in the format (uint32 max) */
52
+ export const MAX_CHUNK_SIZE = 0xffff_ffff;
53
+ /** Maximum permitted total_chunks (sane upper bound, well below uint64 max) */
54
+ export const MAX_TOTAL_CHUNKS = 1_000_000_000;
55
+ /** Domain separators for HKDF — never reuse a string here in another context */
56
+ export const HKDF_INFO = Object.freeze({
57
+ HEADER_MAC: 'shieldfive/v1/header-mac',
58
+ AES_GCM_CHUNK_KEY: 'shieldfive/v1/aes-gcm/chunk-key',
59
+ AES_GCM_NONCE_PREFIX: 'shieldfive/v1/aes-gcm/nonce-prefix',
60
+ AES_GCM_V2_NONCE_PREFIX: 'shieldfive/v1/aes-gcm-v2/nonce-prefix',
61
+ XCHACHA_CHUNK_KEY: 'shieldfive/v1/xchacha/chunk-key',
62
+ XCHACHA_NONCE_PREFIX: 'shieldfive/v1/xchacha/nonce-prefix',
63
+ PQ_HYBRID_COMBINE: 'shieldfive/v1/pq-hybrid/combine',
64
+ ML_KEM_1024_SEED: 'shieldfive/v1/pq-hybrid/ml-kem-1024-seed',
65
+ ARGON2ID_SALT_COMPRESSION: 'shieldfive/v1/argon2id/salt-compression',
66
+ });
67
+ /** AAD domain string mixed into every chunk's authenticator */
68
+ export const CHUNK_AAD_DOMAIN = 'shieldfive/v1/chunk';
69
+ export const CHUNK_AAD_LENGTH = CHUNK_AAD_DOMAIN.length + 8 + 8 + 1;
70
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/internal/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AASH;;;GAGG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG;IACnB,cAAc,EAAE,IAAI;IACpB,qBAAqB,EAAE,IAAI;IAC3B,8BAA8B,EAAE,IAAI;IACpC,cAAc,EAAE,IAAI;CACZ,CAAA;AAUV,MAAM,CAAC,MAAM,WAAW,GAA+B;IACrD,CAAC,KAAK,CAAC,cAAc,CAAC,EAAE,gBAAgB;IACxC,CAAC,KAAK,CAAC,qBAAqB,CAAC,EAAE,uBAAuB;IACtD,CAAC,KAAK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IAC1D,uBAAuB,EAAE,uCAAuC;IAChE,iBAAiB,EAAE,iCAAiC;IACpD,oBAAoB,EAAE,oCAAoC;IAC1D,iBAAiB,EAAE,iCAAiC;IACpD,gBAAgB,EAAE,0CAA0C;IAC5D,yBAAyB,EAAE,yCAAyC;CACrE,CAAC,CAAA;AAEF,+DAA+D;AAC/D,MAAM,CAAC,MAAM,gBAAgB,GAAG,qBAAqB,CAAA;AACrD,MAAM,CAAC,MAAM,gBAAgB,GAAG,gBAAgB,CAAC,MAAM,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA"}
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Argon2id-based key derivation for password-derived master secrets.
3
+ *
4
+ * The library does not require its callers to use this module — the host
5
+ * application can produce a 32-byte master secret any way it likes. But
6
+ * for the common case of "stretch a user passphrase into a master key,"
7
+ * this module gives a vetted default with explicit parameter choices.
8
+ *
9
+ * Backed by libsodium-wrappers-sumo (loaded lazily). Requires the
10
+ * optional peer dependency to be installed.
11
+ */
12
+ /**
13
+ * Argon2id parameter presets.
14
+ *
15
+ * "interactive" is libsodium's MIN; we deliberately do not expose it.
16
+ * "moderate" is the documented default for password storage on
17
+ * contemporary devices (3 ops, 256 MiB).
18
+ * "sensitive" is appropriate for high-value secrets but requires 1 GiB
19
+ * of RAM and is unsuitable for mobile devices.
20
+ *
21
+ * The chosen preset is encoded into the output so a derived secret can be
22
+ * re-derived later without storing the parameters alongside.
23
+ */
24
+ export type Argon2idPreset = 'moderate' | 'sensitive';
25
+ export declare const ARGON2ID_SALT_BYTES = 16;
26
+ export declare const MASTER_SECRET_BYTES = 32;
27
+ export interface DeriveMasterSecretInput {
28
+ /** User passphrase. UTF-8 encoded internally. */
29
+ passphrase: string;
30
+ /** Per-user salt. Must be ≥16 bytes. Generated if not provided. */
31
+ salt?: Uint8Array;
32
+ /** Argon2id strength preset. Default: 'moderate'. */
33
+ preset?: Argon2idPreset;
34
+ }
35
+ export interface DeriveMasterSecretOutput {
36
+ /** 32-byte master secret. Treat as the highest-value secret in the system. */
37
+ masterSecret: Uint8Array;
38
+ /** Salt used (caller must persist this for re-derivation). */
39
+ salt: Uint8Array;
40
+ /** Preset used. Encoded so a future re-derivation produces the same key. */
41
+ preset: Argon2idPreset;
42
+ }
43
+ /**
44
+ * Derive a 32-byte master secret from a passphrase using Argon2id.
45
+ *
46
+ * The host application MUST persist `salt` and `preset` alongside the
47
+ * user's account so that future logins can reproduce the same master
48
+ * secret. The passphrase itself MUST NOT be stored.
49
+ */
50
+ export declare function deriveMasterSecret(input: DeriveMasterSecretInput): Promise<DeriveMasterSecretOutput>;
51
+ /**
52
+ * Re-derive the same master secret given the previously stored salt and
53
+ * preset. Convenience wrapper; exactly equivalent to deriveMasterSecret
54
+ * called with the saved parameters.
55
+ */
56
+ export declare function reDeriveMasterSecret(input: {
57
+ passphrase: string;
58
+ salt: Uint8Array;
59
+ preset: Argon2idPreset;
60
+ }): Promise<Uint8Array>;
61
+ /**
62
+ * Verify a passphrase against a previously derived master secret without
63
+ * exposing whether the input was wrong before the constant-time compare.
64
+ * Returns true iff the rederived master secret matches.
65
+ */
66
+ export declare function verifyPassphrase(input: {
67
+ passphrase: string;
68
+ salt: Uint8Array;
69
+ preset: Argon2idPreset;
70
+ expectedMasterSecret: Uint8Array;
71
+ }): Promise<boolean>;
72
+ //# sourceMappingURL=argon2id.d.ts.map