@shieldfive/crypto 1.0.0-alpha.9 → 1.0.0-beta.1

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 (65) hide show
  1. package/CHANGELOG.md +129 -0
  2. package/README.md +32 -15
  3. package/SECURITY.md +1 -1
  4. package/dist/identity/index.d.ts +16 -0
  5. package/dist/identity/index.d.ts.map +1 -1
  6. package/dist/identity/index.js +84 -22
  7. package/dist/identity/index.js.map +1 -1
  8. package/dist/identity/sign.d.ts +92 -0
  9. package/dist/identity/sign.d.ts.map +1 -0
  10. package/dist/identity/sign.js +182 -0
  11. package/dist/identity/sign.js.map +1 -0
  12. package/dist/index.d.ts +10 -4
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +26 -4
  15. package/dist/index.js.map +1 -1
  16. package/dist/internal/encoding.d.ts.map +1 -1
  17. package/dist/internal/encoding.js +0 -1
  18. package/dist/internal/encoding.js.map +1 -1
  19. package/dist/internal/types.d.ts +4 -1
  20. package/dist/internal/types.d.ts.map +1 -1
  21. package/dist/internal/types.js +8 -0
  22. package/dist/internal/types.js.map +1 -1
  23. package/dist/streams/aes-gcm-v1.d.ts +71 -3
  24. package/dist/streams/aes-gcm-v1.d.ts.map +1 -1
  25. package/dist/streams/aes-gcm-v1.js +183 -49
  26. package/dist/streams/aes-gcm-v1.js.map +1 -1
  27. package/dist/streams/pq-hybrid-v1.d.ts +34 -3
  28. package/dist/streams/pq-hybrid-v1.d.ts.map +1 -1
  29. package/dist/streams/pq-hybrid-v1.js +114 -49
  30. package/dist/streams/pq-hybrid-v1.js.map +1 -1
  31. package/dist/suites/aes-gcm-v1/api.d.ts.map +1 -1
  32. package/dist/suites/aes-gcm-v1/api.js +1 -1
  33. package/dist/suites/aes-gcm-v1/api.js.map +1 -1
  34. package/dist/suites/aes-gcm-v1/index.d.ts +3 -0
  35. package/dist/suites/aes-gcm-v1/index.d.ts.map +1 -1
  36. package/dist/suites/aes-gcm-v1/index.js +3 -0
  37. package/dist/suites/aes-gcm-v1/index.js.map +1 -1
  38. package/dist/suites/aes-gcm-v2/api.d.ts +44 -0
  39. package/dist/suites/aes-gcm-v2/api.d.ts.map +1 -0
  40. package/dist/suites/aes-gcm-v2/api.js +162 -0
  41. package/dist/suites/aes-gcm-v2/api.js.map +1 -0
  42. package/dist/suites/aes-gcm-v2/index.d.ts +63 -0
  43. package/dist/suites/aes-gcm-v2/index.d.ts.map +1 -0
  44. package/dist/suites/aes-gcm-v2/index.js +193 -0
  45. package/dist/suites/aes-gcm-v2/index.js.map +1 -0
  46. package/dist/suites/pq-hybrid-v1/index.d.ts +4 -0
  47. package/dist/suites/pq-hybrid-v1/index.d.ts.map +1 -1
  48. package/dist/suites/pq-hybrid-v1/index.js +15 -1
  49. package/dist/suites/pq-hybrid-v1/index.js.map +1 -1
  50. package/package.json +12 -2
  51. package/spec/format-v1.md +200 -7
  52. package/spec/key-derivation.md +18 -2
  53. package/spec/threat-model.md +36 -21
  54. package/src/identity/index.ts +107 -28
  55. package/src/identity/sign.ts +221 -0
  56. package/src/index.ts +32 -5
  57. package/src/internal/encoding.ts +0 -1
  58. package/src/internal/types.ts +9 -0
  59. package/src/streams/aes-gcm-v1.ts +263 -59
  60. package/src/streams/pq-hybrid-v1.ts +169 -59
  61. package/src/suites/aes-gcm-v1/api.ts +0 -1
  62. package/src/suites/aes-gcm-v1/index.ts +3 -0
  63. package/src/suites/aes-gcm-v2/api.ts +268 -0
  64. package/src/suites/aes-gcm-v2/index.ts +289 -0
  65. package/src/suites/pq-hybrid-v1/index.ts +25 -4
@@ -0,0 +1,221 @@
1
+ /**
2
+ * Sender-attribution signatures over the v1 wire format.
3
+ *
4
+ * The v1 file format already authenticates with HMAC under the content
5
+ * key (`header_mac`) and per-chunk AEAD tags, so a recipient can be sure
6
+ * the file decrypts to bytes the encryptor produced. What it does NOT
7
+ * tell them is WHO encrypted it — anyone with the content key could have
8
+ * written the file.
9
+ *
10
+ * This module adds an optional detached signature over
11
+ * `header_unauthenticated_bytes || concat(per-chunk MAC tags)` so that a
12
+ * recipient who trusts a sender's public key can verify authorship. The
13
+ * signature lives in an optional trailing structure (see
14
+ * `spec/format-v1.md § "Signature block"`); legacy files without that
15
+ * structure continue to decrypt unchanged, with the receiving side
16
+ * reporting `signature: null`.
17
+ *
18
+ * Algorithm 0x01 (Ed25519) is the only algorithm defined in v1. 0x02 is
19
+ * reserved for ML-DSA-65; the format leaves room for it but does not yet
20
+ * implement it.
21
+ */
22
+
23
+ import { ed25519 } from '@noble/curves/ed25519.js'
24
+
25
+ import {
26
+ concatBytes,
27
+ readUint16BE,
28
+ uint16BE,
29
+ } from '../internal/encoding.js'
30
+
31
+ /** Signature algorithm: Ed25519 (RFC 8032). 32-byte key, 64-byte signature. */
32
+ export const SIGNATURE_ALGO_ED25519 = 0x01
33
+
34
+ /** Signature algorithm reserved for ML-DSA-65 (FIPS 204). Not yet implemented. */
35
+ export const SIGNATURE_ALGO_ML_DSA_65 = 0x02
36
+
37
+ export const ED25519_PUBLIC_KEY_LENGTH = 32
38
+ export const ED25519_SECRET_KEY_LENGTH = 32
39
+ export const ED25519_SIGNATURE_LENGTH = 64
40
+
41
+ export interface SignatureBlock {
42
+ /** Algorithm identifier; see `SIGNATURE_ALGO_*`. */
43
+ algorithm: number
44
+ /** Sender's verifying key. Length depends on the algorithm. */
45
+ publicKey: Uint8Array
46
+ /** Detached signature over `signed_message`. */
47
+ signature: Uint8Array
48
+ }
49
+
50
+ export class SignatureError extends Error {
51
+ readonly code: string
52
+ constructor(code: string) {
53
+ super(`shieldfive-crypto: signature error: ${code}`)
54
+ this.name = 'SignatureError'
55
+ this.code = code
56
+ }
57
+ }
58
+
59
+ function buildSignedMessage(
60
+ header: Uint8Array,
61
+ chunkMacs: ReadonlyArray<Uint8Array>,
62
+ ): Uint8Array {
63
+ let totalMacLen = 0
64
+ for (const mac of chunkMacs) totalMacLen += mac.length
65
+ const out = new Uint8Array(header.length + totalMacLen)
66
+ out.set(header, 0)
67
+ let offset = header.length
68
+ for (const mac of chunkMacs) {
69
+ out.set(mac, offset)
70
+ offset += mac.length
71
+ }
72
+ return out
73
+ }
74
+
75
+ /**
76
+ * Produce a detached Ed25519 signature over
77
+ * `header_unauthenticated_bytes || concat(chunk_macs)`.
78
+ *
79
+ * `header` MUST be the exact bytes covered by the file's `header_mac`
80
+ * (i.e. the output of `buildHeaderUnauthenticated` or
81
+ * `parseHeader().unauthenticatedBytes`). `chunkMacs` MUST be the per-chunk
82
+ * AEAD tags in chunk-index order. For suites defined in v1 (AES-GCM-128,
83
+ * ChaCha20-Poly1305) the tag is the trailing 16 bytes of the chunk's
84
+ * ciphertext field.
85
+ *
86
+ * Returns the raw 64-byte signature. Callers wrap it in a signature block
87
+ * via `buildSignatureBlock`.
88
+ */
89
+ export function signHeaderAndMacs(input: {
90
+ ed25519SecretKey: Uint8Array
91
+ header: Uint8Array
92
+ chunkMacs: ReadonlyArray<Uint8Array>
93
+ }): Uint8Array {
94
+ if (input.ed25519SecretKey.length !== ED25519_SECRET_KEY_LENGTH) {
95
+ throw new SignatureError('ed25519_secret_key_wrong_length')
96
+ }
97
+ const message = buildSignedMessage(input.header, input.chunkMacs)
98
+ return ed25519.sign(message, input.ed25519SecretKey)
99
+ }
100
+
101
+ /**
102
+ * Verify a detached Ed25519 signature produced by `signHeaderAndMacs`.
103
+ * Returns `true` on success, `false` on any failure (wrong length,
104
+ * malformed point, bad signature). Never throws on a verification
105
+ * mismatch — failures are not exceptional.
106
+ */
107
+ export function verifyHeaderAndMacs(input: {
108
+ ed25519PublicKey: Uint8Array
109
+ header: Uint8Array
110
+ chunkMacs: ReadonlyArray<Uint8Array>
111
+ signature: Uint8Array
112
+ }): boolean {
113
+ if (input.ed25519PublicKey.length !== ED25519_PUBLIC_KEY_LENGTH) return false
114
+ if (input.signature.length !== ED25519_SIGNATURE_LENGTH) return false
115
+ const message = buildSignedMessage(input.header, input.chunkMacs)
116
+ try {
117
+ return ed25519.verify(input.signature, message, input.ed25519PublicKey)
118
+ } catch {
119
+ // Malformed pubkey or sig encoding — noble throws; we treat as not-verified.
120
+ return false
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Derive the Ed25519 public key from a 32-byte secret seed.
126
+ */
127
+ export function deriveEd25519PublicKey(
128
+ ed25519SecretKey: Uint8Array,
129
+ ): Uint8Array {
130
+ if (ed25519SecretKey.length !== ED25519_SECRET_KEY_LENGTH) {
131
+ throw new SignatureError('ed25519_secret_key_wrong_length')
132
+ }
133
+ return ed25519.getPublicKey(ed25519SecretKey)
134
+ }
135
+
136
+ /**
137
+ * Serialize a signature block:
138
+ * 1 byte algorithm || 2 bytes BE pubkey_len || pubkey || 2 bytes BE sig_len || sig
139
+ */
140
+ export function buildSignatureBlock(block: SignatureBlock): Uint8Array {
141
+ if (
142
+ block.algorithm < 0 ||
143
+ block.algorithm > 0xff ||
144
+ !Number.isInteger(block.algorithm)
145
+ ) {
146
+ throw new SignatureError('algorithm_out_of_range')
147
+ }
148
+ if (block.algorithm === 0x00) {
149
+ throw new SignatureError('algorithm_reserved')
150
+ }
151
+ if (block.publicKey.length > 0xffff) {
152
+ throw new SignatureError('public_key_too_large')
153
+ }
154
+ if (block.signature.length > 0xffff) {
155
+ throw new SignatureError('signature_too_large')
156
+ }
157
+ if (block.algorithm === SIGNATURE_ALGO_ED25519) {
158
+ if (block.publicKey.length !== ED25519_PUBLIC_KEY_LENGTH) {
159
+ throw new SignatureError('ed25519_public_key_wrong_length')
160
+ }
161
+ if (block.signature.length !== ED25519_SIGNATURE_LENGTH) {
162
+ throw new SignatureError('ed25519_signature_wrong_length')
163
+ }
164
+ }
165
+ return concatBytes([
166
+ new Uint8Array([block.algorithm]),
167
+ uint16BE(block.publicKey.length),
168
+ block.publicKey,
169
+ uint16BE(block.signature.length),
170
+ block.signature,
171
+ ])
172
+ }
173
+
174
+ /**
175
+ * Parse a signature block starting at `offset` in `bytes`. Returns the
176
+ * parsed block and the number of bytes consumed. Throws on malformed
177
+ * input (truncation, oversize, or — for known algorithms — wrong key /
178
+ * signature length). Unknown algorithms parse successfully; verification
179
+ * against an unknown algorithm is the caller's policy decision.
180
+ */
181
+ export function parseSignatureBlock(
182
+ bytes: Uint8Array,
183
+ offset = 0,
184
+ ): { block: SignatureBlock; consumed: number } {
185
+ const FIXED_HEADER = 1 + 2 // algorithm + pubkey_len
186
+ if (bytes.length - offset < FIXED_HEADER) {
187
+ throw new SignatureError('block_truncated')
188
+ }
189
+ let pos = offset
190
+ const algorithm = bytes[pos]!
191
+ pos += 1
192
+ if (algorithm === 0x00) {
193
+ throw new SignatureError('algorithm_reserved')
194
+ }
195
+ const pubkeyLen = readUint16BE(bytes, pos)
196
+ pos += 2
197
+ if (bytes.length - pos < pubkeyLen + 2) {
198
+ throw new SignatureError('block_truncated')
199
+ }
200
+ const publicKey = bytes.slice(pos, pos + pubkeyLen)
201
+ pos += pubkeyLen
202
+ const sigLen = readUint16BE(bytes, pos)
203
+ pos += 2
204
+ if (bytes.length - pos < sigLen) {
205
+ throw new SignatureError('block_truncated')
206
+ }
207
+ const signature = bytes.slice(pos, pos + sigLen)
208
+ pos += sigLen
209
+ if (algorithm === SIGNATURE_ALGO_ED25519) {
210
+ if (publicKey.length !== ED25519_PUBLIC_KEY_LENGTH) {
211
+ throw new SignatureError('ed25519_public_key_wrong_length')
212
+ }
213
+ if (signature.length !== ED25519_SIGNATURE_LENGTH) {
214
+ throw new SignatureError('ed25519_signature_wrong_length')
215
+ }
216
+ }
217
+ return {
218
+ block: { algorithm, publicKey, signature },
219
+ consumed: pos - offset,
220
+ }
221
+ }
package/src/index.ts CHANGED
@@ -6,7 +6,8 @@
6
6
  * subpath instead of the bare module name:
7
7
  *
8
8
  * import { encryptBlob, decryptBlob } from '@shieldfive/crypto/pq-hybrid-v1'
9
- * import { encryptBlob, decryptBlob } from '@shieldfive/crypto/aes-gcm-v1'
9
+ * import { encryptBlob, decryptBlob } from '@shieldfive/crypto/aes-gcm-v2'
10
+ * import { decryptBlob } from '@shieldfive/crypto/aes-gcm-v1' // decrypt-only
10
11
  * import { encryptBlob, decryptBlob } from '@shieldfive/crypto/xchacha-v1'
11
12
  * import { decryptV0 } from '@shieldfive/crypto/legacy-v0'
12
13
  *
@@ -74,9 +75,22 @@ export {
74
75
  } from './internal/runtime.js'
75
76
 
76
77
  // ──────────────────────────────────────────────────────────────────────
77
- // Re-export suite namespaces for direct consumption
78
+ // Re-export suite namespaces for direct consumption.
79
+ // The aes-gcm encrypt path on the umbrella is v2 (0x04). v1 (0x01) is
80
+ // exposed decrypt-only here because new files should never be written
81
+ // with the narrow 4-byte nonce prefix. Callers that genuinely need to
82
+ // re-encrypt as v1 (e.g. migration tooling) can still import from the
83
+ // @shieldfive/crypto/aes-gcm-v1 subpath.
78
84
  // ──────────────────────────────────────────────────────────────────────
79
- export * as aesGcmV1 from './suites/aes-gcm-v1/api.js'
85
+ import {
86
+ decryptBlob as aesGcmV1DecryptBlob,
87
+ decryptToBytes as aesGcmV1DecryptToBytes,
88
+ } from './suites/aes-gcm-v1/api.js'
89
+ export const aesGcmV1 = {
90
+ decryptBlob: aesGcmV1DecryptBlob,
91
+ decryptToBytes: aesGcmV1DecryptToBytes,
92
+ }
93
+ export * as aesGcmV2 from './suites/aes-gcm-v2/api.js'
80
94
  export * as xchachaV1 from './suites/xchacha-v1/api.js'
81
95
  export * as pqHybridV1 from './suites/pq-hybrid-v1/api.js'
82
96
  export * as legacyV0 from './suites/aes-gcm-v0/api.js'
@@ -91,12 +105,13 @@ export * as migrationV0 from './migration/v0-bridge.js'
91
105
  import { parseHeader, HeaderError } from './format/header.js'
92
106
  import { SUITE, type SuiteId } from './internal/types.js'
93
107
  import * as aes from './suites/aes-gcm-v1/api.js'
108
+ import * as aesV2 from './suites/aes-gcm-v2/api.js'
94
109
  import * as xchacha from './suites/xchacha-v1/api.js'
95
110
  import * as pqHybrid from './suites/pq-hybrid-v1/api.js'
96
111
 
97
112
  export interface AutoDecryptOptions {
98
113
  blob: Blob
99
- /** Used for AES-GCM-v1 and XChaCha-v1 (32-byte raw content key) */
114
+ /** Used for AES-GCM-v1/v2 and XChaCha-v1 (32-byte raw content key) */
100
115
  contentKey?: Uint8Array
101
116
  /** Required for PQ-hybrid: ML-KEM-1024 secret key (3168 bytes) */
102
117
  recipientSecretKey?: Uint8Array
@@ -157,6 +172,18 @@ export async function autoDecryptBlob(
157
172
  ...(options.onProgress ? { onProgress: options.onProgress } : {}),
158
173
  })
159
174
  }
175
+ case SUITE.AES_256_GCM_V2: {
176
+ if (!options.contentKey) {
177
+ throw new Error(
178
+ 'autoDecryptBlob: contentKey required for aes-256-gcm-v2',
179
+ )
180
+ }
181
+ return aesV2.decryptBlob({
182
+ blob,
183
+ contentKey: options.contentKey,
184
+ ...(options.onProgress ? { onProgress: options.onProgress } : {}),
185
+ })
186
+ }
160
187
  case SUITE.XCHACHA20_POLY1305_V1: {
161
188
  if (!options.contentKey) {
162
189
  throw new Error(
@@ -191,7 +218,7 @@ export async function autoDecryptBlob(
191
218
  }
192
219
 
193
220
  /** Library version, also written into format documentation. */
194
- export const SHIELDFIVE_CRYPTO_VERSION = '1.0.0-alpha.9'
221
+ export const SHIELDFIVE_CRYPTO_VERSION = '1.0.0-beta.1'
195
222
 
196
223
  /** Default cipher suite for new files when not specified by the caller. */
197
224
  export const DEFAULT_SUITE = SUITE.PQ_HYBRID_XCHACHA_MLKEM1024_V1
@@ -26,7 +26,6 @@ export function bytesToBase64(bytes: Uint8Array): string {
26
26
  return btoa(binary)
27
27
  }
28
28
  // Node fallback. Uses Buffer; safe in any Node version.
29
- // eslint-disable-next-line @typescript-eslint/no-require-imports
30
29
  return Buffer.from(bytes).toString('base64')
31
30
  }
32
31
 
@@ -17,6 +17,7 @@ export const SUITE = {
17
17
  AES_256_GCM_V1: 0x01,
18
18
  XCHACHA20_POLY1305_V1: 0x02,
19
19
  PQ_HYBRID_XCHACHA_MLKEM1024_V1: 0x03,
20
+ AES_256_GCM_V2: 0x04,
20
21
  } as const
21
22
 
22
23
  export type SuiteId = (typeof SUITE)[keyof typeof SUITE]
@@ -25,11 +26,13 @@ export type SuiteName =
25
26
  | 'aes-256-gcm-v1'
26
27
  | 'xchacha20-poly1305-v1'
27
28
  | 'pq-hybrid-xchacha-mlkem1024-v1'
29
+ | 'aes-256-gcm-v2'
28
30
 
29
31
  export const SUITE_NAMES: Record<SuiteId, SuiteName> = {
30
32
  [SUITE.AES_256_GCM_V1]: 'aes-256-gcm-v1',
31
33
  [SUITE.XCHACHA20_POLY1305_V1]: 'xchacha20-poly1305-v1',
32
34
  [SUITE.PQ_HYBRID_XCHACHA_MLKEM1024_V1]: 'pq-hybrid-xchacha-mlkem1024-v1',
35
+ [SUITE.AES_256_GCM_V2]: 'aes-256-gcm-v2',
33
36
  }
34
37
 
35
38
  /**
@@ -80,10 +83,16 @@ export const HKDF_INFO = Object.freeze({
80
83
  HEADER_MAC: 'shieldfive/v1/header-mac',
81
84
  AES_GCM_CHUNK_KEY: 'shieldfive/v1/aes-gcm/chunk-key',
82
85
  AES_GCM_NONCE_PREFIX: 'shieldfive/v1/aes-gcm/nonce-prefix',
86
+ AES_GCM_V2_NONCE_PREFIX: 'shieldfive/v1/aes-gcm-v2/nonce-prefix',
83
87
  XCHACHA_CHUNK_KEY: 'shieldfive/v1/xchacha/chunk-key',
84
88
  XCHACHA_NONCE_PREFIX: 'shieldfive/v1/xchacha/nonce-prefix',
85
89
  PQ_HYBRID_COMBINE: 'shieldfive/v1/pq-hybrid/combine',
86
90
  ML_KEM_1024_SEED: 'shieldfive/v1/pq-hybrid/ml-kem-1024-seed',
91
+ // Re-encrypting a file's combined key to a share recipient wraps it under a
92
+ // transport key derived with THIS label, distinct from the file-combiner
93
+ // label above, so a share transport key is never equal to a file's
94
+ // combined key for the same (recipient, envelope, file_id).
95
+ SHARE_TRANSPORT: 'shieldfive/v1/share-transport',
87
96
  ARGON2ID_SALT_COMPRESSION: 'shieldfive/v1/argon2id/salt-compression',
88
97
  })
89
98