@decryption/keys 0.2.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @decryption/keys
2
2
 
3
+ <p align="center" width="100%">
4
+ <img height="250" src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" />
5
+ </p>
6
+
7
+ <p align="center" width="100%">
8
+ <a href="https://github.com/constructive-io/decryption/actions/workflows/ci.yml">
9
+ <img height="20" src="https://github.com/constructive-io/decryption/actions/workflows/ci.yml/badge.svg" />
10
+ </a>
11
+ <a href="https://www.npmjs.com/package/@decryption/keys"><img height="20" src="https://img.shields.io/github/package-json/v/constructive-io/decryption?filename=packages%2Fkeys%2Fpackage.json"/></a>
12
+ </p>
13
+
14
+
3
15
  X25519 identities and recipient-based encryption: encrypt once, let several people decrypt, add or
4
16
  remove them without a new passphrase ceremony. The construction follows
5
17
  [age](https://age-encryption.org): a random file key encrypts the payload, and one *stanza* per
@@ -42,6 +54,24 @@ const identity = identityFromSeed(mnemonicToSeed(mnemonic), 0);
42
54
  Deterministic, so the same BIP39 words always give the same identity — no separate key backup.
43
55
  Use `index` for distinct identities (laptop, CI, break-glass) from one seed.
44
56
 
57
+ ### Files, of any size
58
+
59
+ `sealTo` produces JSON for small values. For files, the same recipient wrapping goes into the binary,
60
+ chunked [version 2 format](../core#file-format-version-2):
61
+
62
+ ```typescript
63
+ import { sealFileTo, openFileAs, createRecipientFileEncryptor } from '@decryption/keys';
64
+
65
+ const file = sealFileTo(bytes, [aliceRecipient, bobRecipient]); // Uint8Array
66
+ openFileAs(file, alice);
67
+
68
+ // streamed: write the header, then the stream's output
69
+ const { header, stream } = createRecipientFileEncryptor([aliceRecipient, bobRecipient]);
70
+ ```
71
+
72
+ `fileRecipients(header)` lists the fingerprints a file is encrypted to without any key, and
73
+ `unwrapWithIdentity` throws `NotRecipientError` when yours is not among them.
74
+
45
75
  ## Format
46
76
 
47
77
  ```typescript
@@ -58,6 +88,16 @@ interface SealedPayload {
58
88
  - `ciphertext` is XChaCha20-Poly1305 via [`@decryption/core`](../core), optionally bound to
59
89
  associated data.
60
90
 
91
+ In a version 2 file each recipient is one 112-byte stanza carrying the same three fields in binary:
92
+
93
+ ```text
94
+ | fingerprint 8 B | ephemeral public key 32 B | wrapped file key 72 B |
95
+ ```
96
+
97
+ None of this — the wrapping, the identity encoding or the keyring — has been audited by a third
98
+ party; only the underlying `@noble` primitives have. See
99
+ [Audit status](../../README.md#audit-status).
100
+
61
101
  Removing a recipient means dropping their stanza and re-sealing under a fresh file key — see
62
102
  [`@decryption/secrets`](../secrets), which does the rekeying for you.
63
103
 
package/esm/file.js ADDED
@@ -0,0 +1,92 @@
1
+ import { CorruptEnvelopeError, createFileDecryptor, createFileEncryptor, generateFileKey, openFile, sealFile, StanzaType, } from '@decryption/core';
2
+ import { bytesToHex, concatBytes, hexToBytes } from '@decryption/hashes/utils';
3
+ import { KeyError, NotRecipientError } from './identity';
4
+ import { normalizeRecipients, unwrapFileKey, wrapFileKey } from './recipients';
5
+ /**
6
+ * X25519 recipients for the version 2 file format of `@decryption/core`.
7
+ *
8
+ * A recipient stanza is the binary form of {@link Stanza}, produced by the same
9
+ * {@link wrapFileKey} that `sealTo` uses:
10
+ *
11
+ * ```text
12
+ * | fingerprint 8 B | ephemeral public key 32 B | wrapped file key 72 B |
13
+ * ```
14
+ */
15
+ const FINGERPRINT_LENGTH = 8;
16
+ const EPHEMERAL_LENGTH = 32;
17
+ const WRAPPED_LENGTH = 24 + 32 + 16;
18
+ const BODY_LENGTH = FINGERPRINT_LENGTH + EPHEMERAL_LENGTH + WRAPPED_LENGTH;
19
+ /** Wraps `fileKey` for one recipient as a file stanza. */
20
+ export const recipientStanza = (fileKey, recipient) => {
21
+ const stanza = wrapFileKey(fileKey, recipient);
22
+ return {
23
+ type: StanzaType.X25519,
24
+ body: concatBytes(hexToBytes(stanza.recipient), hexToBytes(stanza.ephemeral), hexToBytes(stanza.wrapped)),
25
+ };
26
+ };
27
+ const decodeStanza = (stanza) => {
28
+ if (stanza.body.length !== BODY_LENGTH) {
29
+ throw new CorruptEnvelopeError('recipient stanza has the wrong length');
30
+ }
31
+ return {
32
+ recipient: bytesToHex(stanza.body.subarray(0, FINGERPRINT_LENGTH)),
33
+ ephemeral: bytesToHex(stanza.body.subarray(FINGERPRINT_LENGTH, FINGERPRINT_LENGTH + EPHEMERAL_LENGTH)),
34
+ wrapped: bytesToHex(stanza.body.subarray(FINGERPRINT_LENGTH + EPHEMERAL_LENGTH)),
35
+ };
36
+ };
37
+ const recipientStanzas = (header) => header.stanzas.filter((stanza) => stanza.type === StanzaType.X25519).map(decodeStanza);
38
+ /**
39
+ * Fingerprints of the recipients a file is encrypted to, in stanza order. The header is not
40
+ * secret, so this needs no key: it is how tooling reports who can open a file.
41
+ */
42
+ export const fileRecipients = (header) => recipientStanzas(header).map((stanza) => stanza.recipient);
43
+ /**
44
+ * Recovers the file key with `identity`. Throws {@link NotRecipientError} when the identity is
45
+ * not a recipient, including when the file is passphrase-encrypted.
46
+ */
47
+ export const unwrapWithIdentity = (header, identity) => {
48
+ const stanzas = recipientStanzas(header);
49
+ if (stanzas.length === 0) {
50
+ throw new NotRecipientError('this file is not encrypted to any X25519 recipient; it is opened with a passphrase');
51
+ }
52
+ return unwrapFileKey(stanzas, identity);
53
+ };
54
+ /** One stanza per distinct recipient. A file nobody can open is refused. */
55
+ const stanzasFor = (fileKey, recipients) => {
56
+ const unique = normalizeRecipients(recipients);
57
+ if (unique.length === 0)
58
+ throw new KeyError('at least one recipient is required');
59
+ return unique.map((recipient) => recipientStanza(fileKey, recipient));
60
+ };
61
+ /** Starts a streamed file readable by every recipient. Write `header`, then the stream output. */
62
+ export const createRecipientFileEncryptor = (recipients) => {
63
+ const fileKey = generateFileKey();
64
+ try {
65
+ return createFileEncryptor(fileKey, stanzasFor(fileKey, recipients));
66
+ }
67
+ finally {
68
+ fileKey.fill(0);
69
+ }
70
+ };
71
+ /** Verifies the header and returns a streaming decryptor for a file `identity` can open. */
72
+ export const createIdentityFileDecryptor = (header, identity) => {
73
+ const fileKey = unwrapWithIdentity(header, identity);
74
+ try {
75
+ return createFileDecryptor(header, fileKey);
76
+ }
77
+ finally {
78
+ fileKey.fill(0);
79
+ }
80
+ };
81
+ /** Encrypts bytes already in memory into a version 2 file readable by every recipient. */
82
+ export const sealFileTo = (plaintext, recipients) => {
83
+ const fileKey = generateFileKey();
84
+ try {
85
+ return sealFile(plaintext, fileKey, stanzasFor(fileKey, recipients));
86
+ }
87
+ finally {
88
+ fileKey.fill(0);
89
+ }
90
+ };
91
+ /** Inverse of {@link sealFileTo}. */
92
+ export const openFileAs = (file, identity) => openFile(file, (header) => unwrapWithIdentity(header, identity));
package/esm/identity.js CHANGED
@@ -14,6 +14,9 @@ export class KeyError extends Error {
14
14
  this.name = new.target.name;
15
15
  }
16
16
  }
17
+ /** The identity is valid, but nothing was encrypted to it. Distinct so callers can report it. */
18
+ export class NotRecipientError extends KeyError {
19
+ }
17
20
  /** Generates a new X25519 identity from the platform CSPRNG. */
18
21
  export const generateIdentity = () => identityFromPrivateKey(randomBytes(KEY_LENGTH));
19
22
  /** Wraps raw private key bytes as an {@link Identity}. */
package/esm/index.js CHANGED
@@ -1,2 +1,3 @@
1
+ export * from './file';
1
2
  export * from './identity';
2
3
  export * from './recipients';
package/esm/recipients.js CHANGED
@@ -1,12 +1,12 @@
1
- import { openWithKey, sealWithKey, WrongPassphraseError } from '@decryption/core';
1
+ import { generateFileKey, openWithKey, sealWithKey, WrongPassphraseError } from '@decryption/core';
2
2
  import { x25519 } from '@decryption/curves/ed25519';
3
3
  import { hkdf } from '@decryption/hashes/hkdf';
4
4
  import { sha256 } from '@decryption/hashes/sha2';
5
5
  import { bytesToHex, concatBytes, hexToBytes, randomBytes, utf8ToBytes } from '@decryption/hashes/utils';
6
- import { fingerprint, KeyError, recipientFromString, recipientToString } from './identity';
6
+ import { fingerprint, KeyError, NotRecipientError, recipientFromString, recipientToString, } from './identity';
7
7
  const FILE_KEY_LENGTH = 32;
8
- /** Generates a random file key — the symmetric key a payload is actually encrypted under. */
9
- export const generateFileKey = () => randomBytes(FILE_KEY_LENGTH);
8
+ // The file key generator lives in @decryption/core; it is re-exported so this package's API is unchanged.
9
+ export { generateFileKey };
10
10
  /**
11
11
  * Wraps `fileKey` for one recipient using an ephemeral X25519 exchange, exactly the shape age
12
12
  * uses: the wrapping key is `HKDF-SHA256(shared secret, salt = ephemeral ‖ recipient)`.
@@ -33,7 +33,7 @@ export const unwrapFileKey = (stanzas, identity) => {
33
33
  const mine = fingerprint(identity.publicKey);
34
34
  const candidates = stanzas.filter((stanza) => stanza.recipient === mine);
35
35
  if (candidates.length === 0) {
36
- throw new KeyError(`this identity (${mine}) is not a recipient; recipients are ${stanzas.map((s) => s.recipient).join(', ') || 'none'}`);
36
+ throw new NotRecipientError(`this identity (${mine}) is not a recipient; recipients are ${stanzas.map((s) => s.recipient).join(', ') || 'none'}`);
37
37
  }
38
38
  for (const stanza of candidates) {
39
39
  const ephemeralPublic = hexToBytes(stanza.ephemeral);
package/file.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { FileEncryptor, FileHeader, FileStanza, StreamDecryptor } from '@decryption/core';
2
+ import { Identity } from './identity';
3
+ /** Wraps `fileKey` for one recipient as a file stanza. */
4
+ export declare const recipientStanza: (fileKey: Uint8Array, recipient: Uint8Array | string) => FileStanza;
5
+ /**
6
+ * Fingerprints of the recipients a file is encrypted to, in stanza order. The header is not
7
+ * secret, so this needs no key: it is how tooling reports who can open a file.
8
+ */
9
+ export declare const fileRecipients: (header: FileHeader) => string[];
10
+ /**
11
+ * Recovers the file key with `identity`. Throws {@link NotRecipientError} when the identity is
12
+ * not a recipient, including when the file is passphrase-encrypted.
13
+ */
14
+ export declare const unwrapWithIdentity: (header: FileHeader, identity: Identity) => Uint8Array;
15
+ /** Starts a streamed file readable by every recipient. Write `header`, then the stream output. */
16
+ export declare const createRecipientFileEncryptor: (recipients: (Uint8Array | string)[]) => FileEncryptor;
17
+ /** Verifies the header and returns a streaming decryptor for a file `identity` can open. */
18
+ export declare const createIdentityFileDecryptor: (header: FileHeader, identity: Identity) => StreamDecryptor;
19
+ /** Encrypts bytes already in memory into a version 2 file readable by every recipient. */
20
+ export declare const sealFileTo: (plaintext: Uint8Array | string, recipients: (Uint8Array | string)[]) => Uint8Array;
21
+ /** Inverse of {@link sealFileTo}. */
22
+ export declare const openFileAs: (file: Uint8Array, identity: Identity) => Uint8Array;
package/file.js ADDED
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.openFileAs = exports.sealFileTo = exports.createIdentityFileDecryptor = exports.createRecipientFileEncryptor = exports.unwrapWithIdentity = exports.fileRecipients = exports.recipientStanza = void 0;
4
+ const core_1 = require("@decryption/core");
5
+ const utils_1 = require("@decryption/hashes/utils");
6
+ const identity_1 = require("./identity");
7
+ const recipients_1 = require("./recipients");
8
+ /**
9
+ * X25519 recipients for the version 2 file format of `@decryption/core`.
10
+ *
11
+ * A recipient stanza is the binary form of {@link Stanza}, produced by the same
12
+ * {@link wrapFileKey} that `sealTo` uses:
13
+ *
14
+ * ```text
15
+ * | fingerprint 8 B | ephemeral public key 32 B | wrapped file key 72 B |
16
+ * ```
17
+ */
18
+ const FINGERPRINT_LENGTH = 8;
19
+ const EPHEMERAL_LENGTH = 32;
20
+ const WRAPPED_LENGTH = 24 + 32 + 16;
21
+ const BODY_LENGTH = FINGERPRINT_LENGTH + EPHEMERAL_LENGTH + WRAPPED_LENGTH;
22
+ /** Wraps `fileKey` for one recipient as a file stanza. */
23
+ const recipientStanza = (fileKey, recipient) => {
24
+ const stanza = (0, recipients_1.wrapFileKey)(fileKey, recipient);
25
+ return {
26
+ type: core_1.StanzaType.X25519,
27
+ body: (0, utils_1.concatBytes)((0, utils_1.hexToBytes)(stanza.recipient), (0, utils_1.hexToBytes)(stanza.ephemeral), (0, utils_1.hexToBytes)(stanza.wrapped)),
28
+ };
29
+ };
30
+ exports.recipientStanza = recipientStanza;
31
+ const decodeStanza = (stanza) => {
32
+ if (stanza.body.length !== BODY_LENGTH) {
33
+ throw new core_1.CorruptEnvelopeError('recipient stanza has the wrong length');
34
+ }
35
+ return {
36
+ recipient: (0, utils_1.bytesToHex)(stanza.body.subarray(0, FINGERPRINT_LENGTH)),
37
+ ephemeral: (0, utils_1.bytesToHex)(stanza.body.subarray(FINGERPRINT_LENGTH, FINGERPRINT_LENGTH + EPHEMERAL_LENGTH)),
38
+ wrapped: (0, utils_1.bytesToHex)(stanza.body.subarray(FINGERPRINT_LENGTH + EPHEMERAL_LENGTH)),
39
+ };
40
+ };
41
+ const recipientStanzas = (header) => header.stanzas.filter((stanza) => stanza.type === core_1.StanzaType.X25519).map(decodeStanza);
42
+ /**
43
+ * Fingerprints of the recipients a file is encrypted to, in stanza order. The header is not
44
+ * secret, so this needs no key: it is how tooling reports who can open a file.
45
+ */
46
+ const fileRecipients = (header) => recipientStanzas(header).map((stanza) => stanza.recipient);
47
+ exports.fileRecipients = fileRecipients;
48
+ /**
49
+ * Recovers the file key with `identity`. Throws {@link NotRecipientError} when the identity is
50
+ * not a recipient, including when the file is passphrase-encrypted.
51
+ */
52
+ const unwrapWithIdentity = (header, identity) => {
53
+ const stanzas = recipientStanzas(header);
54
+ if (stanzas.length === 0) {
55
+ throw new identity_1.NotRecipientError('this file is not encrypted to any X25519 recipient; it is opened with a passphrase');
56
+ }
57
+ return (0, recipients_1.unwrapFileKey)(stanzas, identity);
58
+ };
59
+ exports.unwrapWithIdentity = unwrapWithIdentity;
60
+ /** One stanza per distinct recipient. A file nobody can open is refused. */
61
+ const stanzasFor = (fileKey, recipients) => {
62
+ const unique = (0, recipients_1.normalizeRecipients)(recipients);
63
+ if (unique.length === 0)
64
+ throw new identity_1.KeyError('at least one recipient is required');
65
+ return unique.map((recipient) => (0, exports.recipientStanza)(fileKey, recipient));
66
+ };
67
+ /** Starts a streamed file readable by every recipient. Write `header`, then the stream output. */
68
+ const createRecipientFileEncryptor = (recipients) => {
69
+ const fileKey = (0, core_1.generateFileKey)();
70
+ try {
71
+ return (0, core_1.createFileEncryptor)(fileKey, stanzasFor(fileKey, recipients));
72
+ }
73
+ finally {
74
+ fileKey.fill(0);
75
+ }
76
+ };
77
+ exports.createRecipientFileEncryptor = createRecipientFileEncryptor;
78
+ /** Verifies the header and returns a streaming decryptor for a file `identity` can open. */
79
+ const createIdentityFileDecryptor = (header, identity) => {
80
+ const fileKey = (0, exports.unwrapWithIdentity)(header, identity);
81
+ try {
82
+ return (0, core_1.createFileDecryptor)(header, fileKey);
83
+ }
84
+ finally {
85
+ fileKey.fill(0);
86
+ }
87
+ };
88
+ exports.createIdentityFileDecryptor = createIdentityFileDecryptor;
89
+ /** Encrypts bytes already in memory into a version 2 file readable by every recipient. */
90
+ const sealFileTo = (plaintext, recipients) => {
91
+ const fileKey = (0, core_1.generateFileKey)();
92
+ try {
93
+ return (0, core_1.sealFile)(plaintext, fileKey, stanzasFor(fileKey, recipients));
94
+ }
95
+ finally {
96
+ fileKey.fill(0);
97
+ }
98
+ };
99
+ exports.sealFileTo = sealFileTo;
100
+ /** Inverse of {@link sealFileTo}. */
101
+ const openFileAs = (file, identity) => (0, core_1.openFile)(file, (header) => (0, exports.unwrapWithIdentity)(header, identity));
102
+ exports.openFileAs = openFileAs;
package/identity.d.ts CHANGED
@@ -5,6 +5,9 @@ export declare const IDENTITY_PREFIX = "dcryptsec";
5
5
  export declare class KeyError extends Error {
6
6
  constructor(message: string);
7
7
  }
8
+ /** The identity is valid, but nothing was encrypted to it. Distinct so callers can report it. */
9
+ export declare class NotRecipientError extends KeyError {
10
+ }
8
11
  /** An X25519 key pair. The private key is only ever held in memory. */
9
12
  export interface Identity {
10
13
  privateKey: Uint8Array;
package/identity.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fingerprint = exports.identityFromString = exports.identityToString = exports.recipientFromString = exports.recipientToString = exports.identityFromSeed = exports.identityFromPrivateKey = exports.generateIdentity = exports.KeyError = exports.IDENTITY_PREFIX = exports.RECIPIENT_PREFIX = void 0;
3
+ exports.fingerprint = exports.identityFromString = exports.identityToString = exports.recipientFromString = exports.recipientToString = exports.identityFromSeed = exports.identityFromPrivateKey = exports.generateIdentity = exports.NotRecipientError = exports.KeyError = exports.IDENTITY_PREFIX = exports.RECIPIENT_PREFIX = void 0;
4
4
  const base_1 = require("@decryption/base");
5
5
  const ed25519_1 = require("@decryption/curves/ed25519");
6
6
  const hkdf_1 = require("@decryption/hashes/hkdf");
@@ -18,6 +18,10 @@ class KeyError extends Error {
18
18
  }
19
19
  }
20
20
  exports.KeyError = KeyError;
21
+ /** The identity is valid, but nothing was encrypted to it. Distinct so callers can report it. */
22
+ class NotRecipientError extends KeyError {
23
+ }
24
+ exports.NotRecipientError = NotRecipientError;
21
25
  /** Generates a new X25519 identity from the platform CSPRNG. */
22
26
  const generateIdentity = () => (0, exports.identityFromPrivateKey)((0, utils_1.randomBytes)(KEY_LENGTH));
23
27
  exports.generateIdentity = generateIdentity;
package/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
+ export * from './file';
1
2
  export * from './identity';
2
3
  export * from './recipients';
package/index.js CHANGED
@@ -14,5 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./file"), exports);
17
18
  __exportStar(require("./identity"), exports);
18
19
  __exportStar(require("./recipients"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decryption/keys",
3
- "version": "0.2.0",
3
+ "version": "0.5.0",
4
4
  "description": "X25519 identities and age-style recipient encryption for sharing secrets with a team",
5
5
  "author": "Constructive <developers@constructive.io>",
6
6
  "main": "index.js",
@@ -37,13 +37,13 @@
37
37
  "sharing"
38
38
  ],
39
39
  "dependencies": {
40
- "@decryption/base": "0.2.0",
41
- "@decryption/core": "0.2.0",
42
- "@decryption/curves": "0.2.0",
43
- "@decryption/hashes": "0.2.0"
40
+ "@decryption/base": "0.4.0",
41
+ "@decryption/core": "0.5.0",
42
+ "@decryption/curves": "0.4.0",
43
+ "@decryption/hashes": "0.4.0"
44
44
  },
45
45
  "devDependencies": {
46
46
  "makage": "0.3.0"
47
47
  },
48
- "gitHead": "8d85fbfdbb1d57da93e99904a03e6e8d8531d281"
48
+ "gitHead": "9f301de50e0b3f40746c6d23ba5ab8d343869efe"
49
49
  }
package/recipients.d.ts CHANGED
@@ -1,4 +1,6 @@
1
+ import { generateFileKey } from '@decryption/core';
1
2
  import { Identity } from './identity';
3
+ export { generateFileKey };
2
4
  /** One recipient's copy of the file key — the age-style "stanza". */
3
5
  export interface Stanza {
4
6
  /** Fingerprint of the recipient this stanza is for, so unwrapping does not have to guess. */
@@ -14,8 +16,6 @@ export interface SealedPayload {
14
16
  /** Ciphertext under the file key, hex-encoded. */
15
17
  ciphertext: string;
16
18
  }
17
- /** Generates a random file key — the symmetric key a payload is actually encrypted under. */
18
- export declare const generateFileKey: () => Uint8Array;
19
19
  /**
20
20
  * Wraps `fileKey` for one recipient using an ephemeral X25519 exchange, exactly the shape age
21
21
  * uses: the wrapping key is `HKDF-SHA256(shared secret, salt = ephemeral ‖ recipient)`.
package/recipients.js CHANGED
@@ -2,15 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.normalizeRecipients = exports.payloadRecipients = exports.openAs = exports.sealTo = exports.unwrapFileKey = exports.wrapFileKey = exports.generateFileKey = void 0;
4
4
  const core_1 = require("@decryption/core");
5
+ Object.defineProperty(exports, "generateFileKey", { enumerable: true, get: function () { return core_1.generateFileKey; } });
5
6
  const ed25519_1 = require("@decryption/curves/ed25519");
6
7
  const hkdf_1 = require("@decryption/hashes/hkdf");
7
8
  const sha2_1 = require("@decryption/hashes/sha2");
8
9
  const utils_1 = require("@decryption/hashes/utils");
9
10
  const identity_1 = require("./identity");
10
11
  const FILE_KEY_LENGTH = 32;
11
- /** Generates a random file key — the symmetric key a payload is actually encrypted under. */
12
- const generateFileKey = () => (0, utils_1.randomBytes)(FILE_KEY_LENGTH);
13
- exports.generateFileKey = generateFileKey;
14
12
  /**
15
13
  * Wraps `fileKey` for one recipient using an ephemeral X25519 exchange, exactly the shape age
16
14
  * uses: the wrapping key is `HKDF-SHA256(shared secret, salt = ephemeral ‖ recipient)`.
@@ -38,7 +36,7 @@ const unwrapFileKey = (stanzas, identity) => {
38
36
  const mine = (0, identity_1.fingerprint)(identity.publicKey);
39
37
  const candidates = stanzas.filter((stanza) => stanza.recipient === mine);
40
38
  if (candidates.length === 0) {
41
- throw new identity_1.KeyError(`this identity (${mine}) is not a recipient; recipients are ${stanzas.map((s) => s.recipient).join(', ') || 'none'}`);
39
+ throw new identity_1.NotRecipientError(`this identity (${mine}) is not a recipient; recipients are ${stanzas.map((s) => s.recipient).join(', ') || 'none'}`);
42
40
  }
43
41
  for (const stanza of candidates) {
44
42
  const ephemeralPublic = (0, utils_1.hexToBytes)(stanza.ephemeral);
@@ -60,7 +58,7 @@ exports.unwrapFileKey = unwrapFileKey;
60
58
  const sealTo = (plaintext, recipients, aad) => {
61
59
  if (recipients.length === 0)
62
60
  throw new identity_1.KeyError('at least one recipient is required');
63
- const fileKey = (0, exports.generateFileKey)();
61
+ const fileKey = (0, core_1.generateFileKey)();
64
62
  try {
65
63
  return {
66
64
  stanzas: recipients.map((recipient) => (0, exports.wrapFileKey)(fileKey, recipient)),