@decryption/keys 0.4.0 → 0.5.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.
- package/README.md +41 -0
- package/esm/file.js +92 -0
- package/esm/identity.js +3 -0
- package/esm/index.js +1 -0
- package/esm/recipients.js +5 -5
- package/file.d.ts +22 -0
- package/file.js +102 -0
- package/identity.d.ts +3 -0
- package/identity.js +5 -1
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +3 -3
- package/recipients.d.ts +2 -2
- package/recipients.js +3 -5
package/README.md
CHANGED
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
<a href="https://github.com/constructive-io/decryption/actions/workflows/ci.yml">
|
|
9
9
|
<img height="20" src="https://github.com/constructive-io/decryption/actions/workflows/ci.yml/badge.svg" />
|
|
10
10
|
</a>
|
|
11
|
+
<a href="https://github.com/constructive-io/decryption/blob/main/LICENSE">
|
|
12
|
+
<img height="20" alt="MIT license" src="https://img.shields.io/badge/license-MIT-blue.svg" />
|
|
13
|
+
</a>
|
|
11
14
|
<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
15
|
</p>
|
|
13
16
|
|
|
@@ -54,6 +57,24 @@ const identity = identityFromSeed(mnemonicToSeed(mnemonic), 0);
|
|
|
54
57
|
Deterministic, so the same BIP39 words always give the same identity — no separate key backup.
|
|
55
58
|
Use `index` for distinct identities (laptop, CI, break-glass) from one seed.
|
|
56
59
|
|
|
60
|
+
### Files, of any size
|
|
61
|
+
|
|
62
|
+
`sealTo` produces JSON for small values. For files, the same recipient wrapping goes into the binary,
|
|
63
|
+
chunked [version 2 format](../core#file-format-version-2):
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import { sealFileTo, openFileAs, createRecipientFileEncryptor } from '@decryption/keys';
|
|
67
|
+
|
|
68
|
+
const file = sealFileTo(bytes, [aliceRecipient, bobRecipient]); // Uint8Array
|
|
69
|
+
openFileAs(file, alice);
|
|
70
|
+
|
|
71
|
+
// streamed: write the header, then the stream's output
|
|
72
|
+
const { header, stream } = createRecipientFileEncryptor([aliceRecipient, bobRecipient]);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
`fileRecipients(header)` lists the fingerprints a file is encrypted to without any key, and
|
|
76
|
+
`unwrapWithIdentity` throws `NotRecipientError` when yours is not among them.
|
|
77
|
+
|
|
57
78
|
## Format
|
|
58
79
|
|
|
59
80
|
```typescript
|
|
@@ -70,9 +91,29 @@ interface SealedPayload {
|
|
|
70
91
|
- `ciphertext` is XChaCha20-Poly1305 via [`@decryption/core`](../core), optionally bound to
|
|
71
92
|
associated data.
|
|
72
93
|
|
|
94
|
+
In a version 2 file each recipient is one 112-byte stanza carrying the same three fields in binary:
|
|
95
|
+
|
|
96
|
+
```text
|
|
97
|
+
| fingerprint 8 B | ephemeral public key 32 B | wrapped file key 72 B |
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
None of this — the wrapping, the identity encoding or the keyring — has been audited by a third
|
|
101
|
+
party; only the underlying `@noble` primitives have. See
|
|
102
|
+
[Audit status](../../README.md#audit-status).
|
|
103
|
+
|
|
73
104
|
Removing a recipient means dropping their stanza and re-sealing under a fresh file key — see
|
|
74
105
|
[`@decryption/secrets`](../secrets), which does the rekeying for you.
|
|
75
106
|
|
|
107
|
+
## Credits
|
|
108
|
+
|
|
109
|
+
**🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
|
|
110
|
+
|
|
111
|
+
## Disclaimer
|
|
112
|
+
|
|
113
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
114
|
+
|
|
115
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
|
116
|
+
|
|
76
117
|
---
|
|
77
118
|
|
|
78
119
|
## Development
|
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
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
|
-
|
|
9
|
-
export
|
|
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
|
|
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
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.
|
|
3
|
+
"version": "0.5.1",
|
|
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",
|
|
@@ -38,12 +38,12 @@
|
|
|
38
38
|
],
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@decryption/base": "0.4.0",
|
|
41
|
-
"@decryption/core": "0.
|
|
41
|
+
"@decryption/core": "0.5.1",
|
|
42
42
|
"@decryption/curves": "0.4.0",
|
|
43
43
|
"@decryption/hashes": "0.4.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"makage": "0.3.0"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "d29d5f2e516edf049a285b30f868360d3b996db0"
|
|
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.
|
|
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,
|
|
61
|
+
const fileKey = (0, core_1.generateFileKey)();
|
|
64
62
|
try {
|
|
65
63
|
return {
|
|
66
64
|
stanzas: recipients.map((recipient) => (0, exports.wrapFileKey)(fileKey, recipient)),
|