@decryption/keys 0.2.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/LICENSE +21 -0
- package/README.md +99 -0
- package/esm/identity.js +59 -0
- package/esm/index.js +2 -0
- package/esm/recipients.js +86 -0
- package/identity.d.ts +32 -0
- package/identity.js +71 -0
- package/index.d.ts +2 -0
- package/index.js +18 -0
- package/package.json +49 -0
- package/recipients.d.ts +33 -0
- package/recipients.js +96 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Constructive <developers@constructive.io>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# @decryption/keys
|
|
2
|
+
|
|
3
|
+
X25519 identities and recipient-based encryption: encrypt once, let several people decrypt, add or
|
|
4
|
+
remove them without a new passphrase ceremony. The construction follows
|
|
5
|
+
[age](https://age-encryption.org): a random file key encrypts the payload, and one *stanza* per
|
|
6
|
+
recipient wraps that file key under an ephemeral X25519 exchange.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @decryption/keys
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import {
|
|
18
|
+
generateIdentity,
|
|
19
|
+
identityToString,
|
|
20
|
+
recipientToString,
|
|
21
|
+
sealTo,
|
|
22
|
+
openAs,
|
|
23
|
+
} from '@decryption/keys';
|
|
24
|
+
|
|
25
|
+
const alice = generateIdentity();
|
|
26
|
+
recipientToString(alice.publicKey); // dcrypt1… — share this
|
|
27
|
+
identityToString(alice); // DCRYPTSEC1… — keep this secret
|
|
28
|
+
|
|
29
|
+
const sealed = sealTo('super secret', [recipientToString(alice.publicKey), bobRecipient]);
|
|
30
|
+
new TextDecoder().decode(openAs(sealed, alice)); // 'super secret'
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Recovering an identity from a mnemonic
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { identityFromSeed } from '@decryption/keys';
|
|
37
|
+
import { mnemonicToSeed } from '@decryption/wallet';
|
|
38
|
+
|
|
39
|
+
const identity = identityFromSeed(mnemonicToSeed(mnemonic), 0);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Deterministic, so the same BIP39 words always give the same identity — no separate key backup.
|
|
43
|
+
Use `index` for distinct identities (laptop, CI, break-glass) from one seed.
|
|
44
|
+
|
|
45
|
+
## Format
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
interface SealedPayload {
|
|
49
|
+
stanzas: { recipient: string; ephemeral: string; wrapped: string }[];
|
|
50
|
+
ciphertext: string;
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
- `recipient` is a fingerprint (truncated SHA-256 of the public key), not the key itself, so a
|
|
55
|
+
sealed file does not publish the full recipient list.
|
|
56
|
+
- `wrapped` is the file key sealed under
|
|
57
|
+
`HKDF-SHA256(X25519(ephemeral, recipient), salt = ephemeral ‖ recipient, info = "dcrypt-recipient-v1")`.
|
|
58
|
+
- `ciphertext` is XChaCha20-Poly1305 via [`@decryption/core`](../core), optionally bound to
|
|
59
|
+
associated data.
|
|
60
|
+
|
|
61
|
+
Removing a recipient means dropping their stanza and re-sealing under a fresh file key — see
|
|
62
|
+
[`@decryption/secrets`](../secrets), which does the rekeying for you.
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## Development
|
|
67
|
+
|
|
68
|
+
### Setup
|
|
69
|
+
|
|
70
|
+
1. Clone the repository:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
git clone https://github.com/constructive-io/decryption.git
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
2. Install dependencies:
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
cd decryption
|
|
80
|
+
pnpm install
|
|
81
|
+
pnpm build
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
3. Test the package of interest:
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
cd packages/<packagename>
|
|
88
|
+
pnpm test:watch
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Credits
|
|
92
|
+
|
|
93
|
+
**🛠 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).**
|
|
94
|
+
|
|
95
|
+
## Disclaimer
|
|
96
|
+
|
|
97
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
98
|
+
|
|
99
|
+
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.
|
package/esm/identity.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { bech32 } from '@decryption/base';
|
|
2
|
+
import { x25519 } from '@decryption/curves/ed25519';
|
|
3
|
+
import { hkdf } from '@decryption/hashes/hkdf';
|
|
4
|
+
import { sha256 } from '@decryption/hashes/sha2';
|
|
5
|
+
import { bytesToHex, randomBytes, utf8ToBytes } from '@decryption/hashes/utils';
|
|
6
|
+
/** Bech32 prefix of a public recipient string: `dcrypt1…`. */
|
|
7
|
+
export const RECIPIENT_PREFIX = 'dcrypt';
|
|
8
|
+
/** Bech32 prefix of a private identity string: `dcryptsec1…`. */
|
|
9
|
+
export const IDENTITY_PREFIX = 'dcryptsec';
|
|
10
|
+
const KEY_LENGTH = 32;
|
|
11
|
+
export class KeyError extends Error {
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = new.target.name;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/** Generates a new X25519 identity from the platform CSPRNG. */
|
|
18
|
+
export const generateIdentity = () => identityFromPrivateKey(randomBytes(KEY_LENGTH));
|
|
19
|
+
/** Wraps raw private key bytes as an {@link Identity}. */
|
|
20
|
+
export const identityFromPrivateKey = (privateKey) => {
|
|
21
|
+
if (privateKey.length !== KEY_LENGTH) {
|
|
22
|
+
throw new KeyError(`private key must be ${KEY_LENGTH} bytes, got ${privateKey.length}`);
|
|
23
|
+
}
|
|
24
|
+
return { privateKey, publicKey: x25519.getPublicKey(privateKey) };
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Derives an identity deterministically from a BIP39 seed, so a user who keeps their mnemonic can
|
|
28
|
+
* recover their encryption identity without a separate backup. `index` allows several identities
|
|
29
|
+
* (work laptop, CI, break-glass) from one seed.
|
|
30
|
+
*/
|
|
31
|
+
export const identityFromSeed = (seed, index = 0) => identityFromPrivateKey(hkdf(sha256, seed, utf8ToBytes('dcrypt-identity-v1'), utf8ToBytes(`identity/${index}`), KEY_LENGTH));
|
|
32
|
+
/** Encodes the public half as a shareable `dcrypt1…` recipient string. */
|
|
33
|
+
export const recipientToString = (publicKey) => bech32.encode(RECIPIENT_PREFIX, bech32.toWords(publicKey));
|
|
34
|
+
/** Decodes a `dcrypt1…` recipient string. */
|
|
35
|
+
export const recipientFromString = (recipient) => {
|
|
36
|
+
const decoded = decodeBech32(recipient.trim(), RECIPIENT_PREFIX, 'recipient');
|
|
37
|
+
if (decoded.length !== KEY_LENGTH)
|
|
38
|
+
throw new KeyError('recipient key has the wrong length');
|
|
39
|
+
return decoded;
|
|
40
|
+
};
|
|
41
|
+
/** Encodes the private half as a `dcryptsec1…` string. Treat it like a password. */
|
|
42
|
+
export const identityToString = (identity) => bech32.encode(IDENTITY_PREFIX, bech32.toWords(identity.privateKey)).toUpperCase();
|
|
43
|
+
/** Decodes a `dcryptsec1…` string back into an {@link Identity}. */
|
|
44
|
+
export const identityFromString = (encoded) => identityFromPrivateKey(decodeBech32(encoded.trim().toLowerCase(), IDENTITY_PREFIX, 'identity'));
|
|
45
|
+
/** Short, human-comparable fingerprint of a recipient — the first 8 bytes of its SHA-256. */
|
|
46
|
+
export const fingerprint = (publicKey) => bytesToHex(sha256(publicKey)).slice(0, 16);
|
|
47
|
+
const decodeBech32 = (value, prefix, kind) => {
|
|
48
|
+
let decoded;
|
|
49
|
+
try {
|
|
50
|
+
decoded = bech32.decode(value, 256);
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
throw new KeyError(`${kind} string is not valid bech32`);
|
|
54
|
+
}
|
|
55
|
+
if (decoded.prefix !== prefix) {
|
|
56
|
+
throw new KeyError(`expected a "${prefix}1…" ${kind} string, got "${decoded.prefix}1…"`);
|
|
57
|
+
}
|
|
58
|
+
return Uint8Array.from(bech32.fromWords(decoded.words));
|
|
59
|
+
};
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { openWithKey, sealWithKey, WrongPassphraseError } from '@decryption/core';
|
|
2
|
+
import { x25519 } from '@decryption/curves/ed25519';
|
|
3
|
+
import { hkdf } from '@decryption/hashes/hkdf';
|
|
4
|
+
import { sha256 } from '@decryption/hashes/sha2';
|
|
5
|
+
import { bytesToHex, concatBytes, hexToBytes, randomBytes, utf8ToBytes } from '@decryption/hashes/utils';
|
|
6
|
+
import { fingerprint, KeyError, recipientFromString, recipientToString } from './identity';
|
|
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);
|
|
10
|
+
/**
|
|
11
|
+
* Wraps `fileKey` for one recipient using an ephemeral X25519 exchange, exactly the shape age
|
|
12
|
+
* uses: the wrapping key is `HKDF-SHA256(shared secret, salt = ephemeral ‖ recipient)`.
|
|
13
|
+
*/
|
|
14
|
+
export const wrapFileKey = (fileKey, recipient) => {
|
|
15
|
+
const recipientKey = typeof recipient === 'string' ? recipientFromString(recipient) : recipient;
|
|
16
|
+
const ephemeralPrivate = randomBytes(32);
|
|
17
|
+
const ephemeralPublic = x25519.getPublicKey(ephemeralPrivate);
|
|
18
|
+
const wrappingKey = deriveWrappingKey(x25519.getSharedSecret(ephemeralPrivate, recipientKey), ephemeralPublic, recipientKey);
|
|
19
|
+
try {
|
|
20
|
+
return {
|
|
21
|
+
recipient: fingerprint(recipientKey),
|
|
22
|
+
ephemeral: bytesToHex(ephemeralPublic),
|
|
23
|
+
wrapped: bytesToHex(sealWithKey(wrappingKey, fileKey)),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
finally {
|
|
27
|
+
ephemeralPrivate.fill(0);
|
|
28
|
+
wrappingKey.fill(0);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
/** Recovers the file key from the stanza addressed to `identity`. */
|
|
32
|
+
export const unwrapFileKey = (stanzas, identity) => {
|
|
33
|
+
const mine = fingerprint(identity.publicKey);
|
|
34
|
+
const candidates = stanzas.filter((stanza) => stanza.recipient === mine);
|
|
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'}`);
|
|
37
|
+
}
|
|
38
|
+
for (const stanza of candidates) {
|
|
39
|
+
const ephemeralPublic = hexToBytes(stanza.ephemeral);
|
|
40
|
+
const wrappingKey = deriveWrappingKey(x25519.getSharedSecret(identity.privateKey, ephemeralPublic), ephemeralPublic, identity.publicKey);
|
|
41
|
+
try {
|
|
42
|
+
return openWithKey(wrappingKey, hexToBytes(stanza.wrapped));
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
wrappingKey.fill(0);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
throw new WrongPassphraseError('stanza did not unwrap: the file may have been tampered with');
|
|
52
|
+
};
|
|
53
|
+
/** Encrypts `plaintext` once, readable by every recipient. */
|
|
54
|
+
export const sealTo = (plaintext, recipients, aad) => {
|
|
55
|
+
if (recipients.length === 0)
|
|
56
|
+
throw new KeyError('at least one recipient is required');
|
|
57
|
+
const fileKey = generateFileKey();
|
|
58
|
+
try {
|
|
59
|
+
return {
|
|
60
|
+
stanzas: recipients.map((recipient) => wrapFileKey(fileKey, recipient)),
|
|
61
|
+
ciphertext: bytesToHex(sealWithKey(fileKey, plaintext, aad)),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
fileKey.fill(0);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
/** Inverse of {@link sealTo}. */
|
|
69
|
+
export const openAs = (payload, identity, aad) => {
|
|
70
|
+
const fileKey = unwrapFileKey(payload.stanzas, identity);
|
|
71
|
+
try {
|
|
72
|
+
return openWithKey(fileKey, hexToBytes(payload.ciphertext), aad);
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
fileKey.fill(0);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
/** Recipient strings a payload can be opened by, in stanza order (fingerprints only). */
|
|
79
|
+
export const payloadRecipients = (payload) => payload.stanzas.map((stanza) => stanza.recipient);
|
|
80
|
+
/** Normalizes a mixed list of recipient strings/keys into canonical `dcrypt1…` strings. */
|
|
81
|
+
export const normalizeRecipients = (recipients) => [
|
|
82
|
+
...new Set(recipients.map((recipient) => typeof recipient === 'string'
|
|
83
|
+
? recipientToString(recipientFromString(recipient))
|
|
84
|
+
: recipientToString(recipient))),
|
|
85
|
+
];
|
|
86
|
+
const deriveWrappingKey = (sharedSecret, ephemeralPublic, recipientPublic) => hkdf(sha256, sharedSecret, concatBytes(ephemeralPublic, recipientPublic), utf8ToBytes('dcrypt-recipient-v1'), FILE_KEY_LENGTH);
|
package/identity.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** Bech32 prefix of a public recipient string: `dcrypt1…`. */
|
|
2
|
+
export declare const RECIPIENT_PREFIX = "dcrypt";
|
|
3
|
+
/** Bech32 prefix of a private identity string: `dcryptsec1…`. */
|
|
4
|
+
export declare const IDENTITY_PREFIX = "dcryptsec";
|
|
5
|
+
export declare class KeyError extends Error {
|
|
6
|
+
constructor(message: string);
|
|
7
|
+
}
|
|
8
|
+
/** An X25519 key pair. The private key is only ever held in memory. */
|
|
9
|
+
export interface Identity {
|
|
10
|
+
privateKey: Uint8Array;
|
|
11
|
+
publicKey: Uint8Array;
|
|
12
|
+
}
|
|
13
|
+
/** Generates a new X25519 identity from the platform CSPRNG. */
|
|
14
|
+
export declare const generateIdentity: () => Identity;
|
|
15
|
+
/** Wraps raw private key bytes as an {@link Identity}. */
|
|
16
|
+
export declare const identityFromPrivateKey: (privateKey: Uint8Array) => Identity;
|
|
17
|
+
/**
|
|
18
|
+
* Derives an identity deterministically from a BIP39 seed, so a user who keeps their mnemonic can
|
|
19
|
+
* recover their encryption identity without a separate backup. `index` allows several identities
|
|
20
|
+
* (work laptop, CI, break-glass) from one seed.
|
|
21
|
+
*/
|
|
22
|
+
export declare const identityFromSeed: (seed: Uint8Array, index?: number) => Identity;
|
|
23
|
+
/** Encodes the public half as a shareable `dcrypt1…` recipient string. */
|
|
24
|
+
export declare const recipientToString: (publicKey: Uint8Array) => string;
|
|
25
|
+
/** Decodes a `dcrypt1…` recipient string. */
|
|
26
|
+
export declare const recipientFromString: (recipient: string) => Uint8Array;
|
|
27
|
+
/** Encodes the private half as a `dcryptsec1…` string. Treat it like a password. */
|
|
28
|
+
export declare const identityToString: (identity: Identity) => string;
|
|
29
|
+
/** Decodes a `dcryptsec1…` string back into an {@link Identity}. */
|
|
30
|
+
export declare const identityFromString: (encoded: string) => Identity;
|
|
31
|
+
/** Short, human-comparable fingerprint of a recipient — the first 8 bytes of its SHA-256. */
|
|
32
|
+
export declare const fingerprint: (publicKey: Uint8Array) => string;
|
package/identity.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
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;
|
|
4
|
+
const base_1 = require("@decryption/base");
|
|
5
|
+
const ed25519_1 = require("@decryption/curves/ed25519");
|
|
6
|
+
const hkdf_1 = require("@decryption/hashes/hkdf");
|
|
7
|
+
const sha2_1 = require("@decryption/hashes/sha2");
|
|
8
|
+
const utils_1 = require("@decryption/hashes/utils");
|
|
9
|
+
/** Bech32 prefix of a public recipient string: `dcrypt1…`. */
|
|
10
|
+
exports.RECIPIENT_PREFIX = 'dcrypt';
|
|
11
|
+
/** Bech32 prefix of a private identity string: `dcryptsec1…`. */
|
|
12
|
+
exports.IDENTITY_PREFIX = 'dcryptsec';
|
|
13
|
+
const KEY_LENGTH = 32;
|
|
14
|
+
class KeyError extends Error {
|
|
15
|
+
constructor(message) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = new.target.name;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.KeyError = KeyError;
|
|
21
|
+
/** Generates a new X25519 identity from the platform CSPRNG. */
|
|
22
|
+
const generateIdentity = () => (0, exports.identityFromPrivateKey)((0, utils_1.randomBytes)(KEY_LENGTH));
|
|
23
|
+
exports.generateIdentity = generateIdentity;
|
|
24
|
+
/** Wraps raw private key bytes as an {@link Identity}. */
|
|
25
|
+
const identityFromPrivateKey = (privateKey) => {
|
|
26
|
+
if (privateKey.length !== KEY_LENGTH) {
|
|
27
|
+
throw new KeyError(`private key must be ${KEY_LENGTH} bytes, got ${privateKey.length}`);
|
|
28
|
+
}
|
|
29
|
+
return { privateKey, publicKey: ed25519_1.x25519.getPublicKey(privateKey) };
|
|
30
|
+
};
|
|
31
|
+
exports.identityFromPrivateKey = identityFromPrivateKey;
|
|
32
|
+
/**
|
|
33
|
+
* Derives an identity deterministically from a BIP39 seed, so a user who keeps their mnemonic can
|
|
34
|
+
* recover their encryption identity without a separate backup. `index` allows several identities
|
|
35
|
+
* (work laptop, CI, break-glass) from one seed.
|
|
36
|
+
*/
|
|
37
|
+
const identityFromSeed = (seed, index = 0) => (0, exports.identityFromPrivateKey)((0, hkdf_1.hkdf)(sha2_1.sha256, seed, (0, utils_1.utf8ToBytes)('dcrypt-identity-v1'), (0, utils_1.utf8ToBytes)(`identity/${index}`), KEY_LENGTH));
|
|
38
|
+
exports.identityFromSeed = identityFromSeed;
|
|
39
|
+
/** Encodes the public half as a shareable `dcrypt1…` recipient string. */
|
|
40
|
+
const recipientToString = (publicKey) => base_1.bech32.encode(exports.RECIPIENT_PREFIX, base_1.bech32.toWords(publicKey));
|
|
41
|
+
exports.recipientToString = recipientToString;
|
|
42
|
+
/** Decodes a `dcrypt1…` recipient string. */
|
|
43
|
+
const recipientFromString = (recipient) => {
|
|
44
|
+
const decoded = decodeBech32(recipient.trim(), exports.RECIPIENT_PREFIX, 'recipient');
|
|
45
|
+
if (decoded.length !== KEY_LENGTH)
|
|
46
|
+
throw new KeyError('recipient key has the wrong length');
|
|
47
|
+
return decoded;
|
|
48
|
+
};
|
|
49
|
+
exports.recipientFromString = recipientFromString;
|
|
50
|
+
/** Encodes the private half as a `dcryptsec1…` string. Treat it like a password. */
|
|
51
|
+
const identityToString = (identity) => base_1.bech32.encode(exports.IDENTITY_PREFIX, base_1.bech32.toWords(identity.privateKey)).toUpperCase();
|
|
52
|
+
exports.identityToString = identityToString;
|
|
53
|
+
/** Decodes a `dcryptsec1…` string back into an {@link Identity}. */
|
|
54
|
+
const identityFromString = (encoded) => (0, exports.identityFromPrivateKey)(decodeBech32(encoded.trim().toLowerCase(), exports.IDENTITY_PREFIX, 'identity'));
|
|
55
|
+
exports.identityFromString = identityFromString;
|
|
56
|
+
/** Short, human-comparable fingerprint of a recipient — the first 8 bytes of its SHA-256. */
|
|
57
|
+
const fingerprint = (publicKey) => (0, utils_1.bytesToHex)((0, sha2_1.sha256)(publicKey)).slice(0, 16);
|
|
58
|
+
exports.fingerprint = fingerprint;
|
|
59
|
+
const decodeBech32 = (value, prefix, kind) => {
|
|
60
|
+
let decoded;
|
|
61
|
+
try {
|
|
62
|
+
decoded = base_1.bech32.decode(value, 256);
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
throw new KeyError(`${kind} string is not valid bech32`);
|
|
66
|
+
}
|
|
67
|
+
if (decoded.prefix !== prefix) {
|
|
68
|
+
throw new KeyError(`expected a "${prefix}1…" ${kind} string, got "${decoded.prefix}1…"`);
|
|
69
|
+
}
|
|
70
|
+
return Uint8Array.from(base_1.bech32.fromWords(decoded.words));
|
|
71
|
+
};
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./identity"), exports);
|
|
18
|
+
__exportStar(require("./recipients"), exports);
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@decryption/keys",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "X25519 identities and age-style recipient encryption for sharing secrets with a team",
|
|
5
|
+
"author": "Constructive <developers@constructive.io>",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/constructive-io/decryption",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/constructive-io/decryption"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/constructive-io/decryption/issues"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"copy": "makage assets",
|
|
24
|
+
"clean": "makage clean",
|
|
25
|
+
"prepublishOnly": "npm run build",
|
|
26
|
+
"build": "makage build",
|
|
27
|
+
"lint": "eslint . --fix",
|
|
28
|
+
"test": "jest",
|
|
29
|
+
"test:watch": "jest --watch"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"x25519",
|
|
33
|
+
"age",
|
|
34
|
+
"recipients",
|
|
35
|
+
"keys",
|
|
36
|
+
"identity",
|
|
37
|
+
"sharing"
|
|
38
|
+
],
|
|
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"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"makage": "0.3.0"
|
|
47
|
+
},
|
|
48
|
+
"gitHead": "8d85fbfdbb1d57da93e99904a03e6e8d8531d281"
|
|
49
|
+
}
|
package/recipients.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Identity } from './identity';
|
|
2
|
+
/** One recipient's copy of the file key — the age-style "stanza". */
|
|
3
|
+
export interface Stanza {
|
|
4
|
+
/** Fingerprint of the recipient this stanza is for, so unwrapping does not have to guess. */
|
|
5
|
+
recipient: string;
|
|
6
|
+
/** Ephemeral X25519 public key, hex-encoded. */
|
|
7
|
+
ephemeral: string;
|
|
8
|
+
/** File key sealed to the recipient, hex-encoded. */
|
|
9
|
+
wrapped: string;
|
|
10
|
+
}
|
|
11
|
+
/** A payload sealed once and readable by every listed recipient. */
|
|
12
|
+
export interface SealedPayload {
|
|
13
|
+
stanzas: Stanza[];
|
|
14
|
+
/** Ciphertext under the file key, hex-encoded. */
|
|
15
|
+
ciphertext: string;
|
|
16
|
+
}
|
|
17
|
+
/** Generates a random file key — the symmetric key a payload is actually encrypted under. */
|
|
18
|
+
export declare const generateFileKey: () => Uint8Array;
|
|
19
|
+
/**
|
|
20
|
+
* Wraps `fileKey` for one recipient using an ephemeral X25519 exchange, exactly the shape age
|
|
21
|
+
* uses: the wrapping key is `HKDF-SHA256(shared secret, salt = ephemeral ‖ recipient)`.
|
|
22
|
+
*/
|
|
23
|
+
export declare const wrapFileKey: (fileKey: Uint8Array, recipient: Uint8Array | string) => Stanza;
|
|
24
|
+
/** Recovers the file key from the stanza addressed to `identity`. */
|
|
25
|
+
export declare const unwrapFileKey: (stanzas: Stanza[], identity: Identity) => Uint8Array;
|
|
26
|
+
/** Encrypts `plaintext` once, readable by every recipient. */
|
|
27
|
+
export declare const sealTo: (plaintext: Uint8Array | string, recipients: (Uint8Array | string)[], aad?: string) => SealedPayload;
|
|
28
|
+
/** Inverse of {@link sealTo}. */
|
|
29
|
+
export declare const openAs: (payload: SealedPayload, identity: Identity, aad?: string) => Uint8Array;
|
|
30
|
+
/** Recipient strings a payload can be opened by, in stanza order (fingerprints only). */
|
|
31
|
+
export declare const payloadRecipients: (payload: SealedPayload) => string[];
|
|
32
|
+
/** Normalizes a mixed list of recipient strings/keys into canonical `dcrypt1…` strings. */
|
|
33
|
+
export declare const normalizeRecipients: (recipients: (Uint8Array | string)[]) => string[];
|
package/recipients.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeRecipients = exports.payloadRecipients = exports.openAs = exports.sealTo = exports.unwrapFileKey = exports.wrapFileKey = exports.generateFileKey = void 0;
|
|
4
|
+
const core_1 = require("@decryption/core");
|
|
5
|
+
const ed25519_1 = require("@decryption/curves/ed25519");
|
|
6
|
+
const hkdf_1 = require("@decryption/hashes/hkdf");
|
|
7
|
+
const sha2_1 = require("@decryption/hashes/sha2");
|
|
8
|
+
const utils_1 = require("@decryption/hashes/utils");
|
|
9
|
+
const identity_1 = require("./identity");
|
|
10
|
+
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
|
+
/**
|
|
15
|
+
* Wraps `fileKey` for one recipient using an ephemeral X25519 exchange, exactly the shape age
|
|
16
|
+
* uses: the wrapping key is `HKDF-SHA256(shared secret, salt = ephemeral ‖ recipient)`.
|
|
17
|
+
*/
|
|
18
|
+
const wrapFileKey = (fileKey, recipient) => {
|
|
19
|
+
const recipientKey = typeof recipient === 'string' ? (0, identity_1.recipientFromString)(recipient) : recipient;
|
|
20
|
+
const ephemeralPrivate = (0, utils_1.randomBytes)(32);
|
|
21
|
+
const ephemeralPublic = ed25519_1.x25519.getPublicKey(ephemeralPrivate);
|
|
22
|
+
const wrappingKey = deriveWrappingKey(ed25519_1.x25519.getSharedSecret(ephemeralPrivate, recipientKey), ephemeralPublic, recipientKey);
|
|
23
|
+
try {
|
|
24
|
+
return {
|
|
25
|
+
recipient: (0, identity_1.fingerprint)(recipientKey),
|
|
26
|
+
ephemeral: (0, utils_1.bytesToHex)(ephemeralPublic),
|
|
27
|
+
wrapped: (0, utils_1.bytesToHex)((0, core_1.sealWithKey)(wrappingKey, fileKey)),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
finally {
|
|
31
|
+
ephemeralPrivate.fill(0);
|
|
32
|
+
wrappingKey.fill(0);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
exports.wrapFileKey = wrapFileKey;
|
|
36
|
+
/** Recovers the file key from the stanza addressed to `identity`. */
|
|
37
|
+
const unwrapFileKey = (stanzas, identity) => {
|
|
38
|
+
const mine = (0, identity_1.fingerprint)(identity.publicKey);
|
|
39
|
+
const candidates = stanzas.filter((stanza) => stanza.recipient === mine);
|
|
40
|
+
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'}`);
|
|
42
|
+
}
|
|
43
|
+
for (const stanza of candidates) {
|
|
44
|
+
const ephemeralPublic = (0, utils_1.hexToBytes)(stanza.ephemeral);
|
|
45
|
+
const wrappingKey = deriveWrappingKey(ed25519_1.x25519.getSharedSecret(identity.privateKey, ephemeralPublic), ephemeralPublic, identity.publicKey);
|
|
46
|
+
try {
|
|
47
|
+
return (0, core_1.openWithKey)(wrappingKey, (0, utils_1.hexToBytes)(stanza.wrapped));
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
wrappingKey.fill(0);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
throw new core_1.WrongPassphraseError('stanza did not unwrap: the file may have been tampered with');
|
|
57
|
+
};
|
|
58
|
+
exports.unwrapFileKey = unwrapFileKey;
|
|
59
|
+
/** Encrypts `plaintext` once, readable by every recipient. */
|
|
60
|
+
const sealTo = (plaintext, recipients, aad) => {
|
|
61
|
+
if (recipients.length === 0)
|
|
62
|
+
throw new identity_1.KeyError('at least one recipient is required');
|
|
63
|
+
const fileKey = (0, exports.generateFileKey)();
|
|
64
|
+
try {
|
|
65
|
+
return {
|
|
66
|
+
stanzas: recipients.map((recipient) => (0, exports.wrapFileKey)(fileKey, recipient)),
|
|
67
|
+
ciphertext: (0, utils_1.bytesToHex)((0, core_1.sealWithKey)(fileKey, plaintext, aad)),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
fileKey.fill(0);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
exports.sealTo = sealTo;
|
|
75
|
+
/** Inverse of {@link sealTo}. */
|
|
76
|
+
const openAs = (payload, identity, aad) => {
|
|
77
|
+
const fileKey = (0, exports.unwrapFileKey)(payload.stanzas, identity);
|
|
78
|
+
try {
|
|
79
|
+
return (0, core_1.openWithKey)(fileKey, (0, utils_1.hexToBytes)(payload.ciphertext), aad);
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
fileKey.fill(0);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
exports.openAs = openAs;
|
|
86
|
+
/** Recipient strings a payload can be opened by, in stanza order (fingerprints only). */
|
|
87
|
+
const payloadRecipients = (payload) => payload.stanzas.map((stanza) => stanza.recipient);
|
|
88
|
+
exports.payloadRecipients = payloadRecipients;
|
|
89
|
+
/** Normalizes a mixed list of recipient strings/keys into canonical `dcrypt1…` strings. */
|
|
90
|
+
const normalizeRecipients = (recipients) => [
|
|
91
|
+
...new Set(recipients.map((recipient) => typeof recipient === 'string'
|
|
92
|
+
? (0, identity_1.recipientToString)((0, identity_1.recipientFromString)(recipient))
|
|
93
|
+
: (0, identity_1.recipientToString)(recipient))),
|
|
94
|
+
];
|
|
95
|
+
exports.normalizeRecipients = normalizeRecipients;
|
|
96
|
+
const deriveWrappingKey = (sharedSecret, ephemeralPublic, recipientPublic) => (0, hkdf_1.hkdf)(sha2_1.sha256, sharedSecret, (0, utils_1.concatBytes)(ephemeralPublic, recipientPublic), (0, utils_1.utf8ToBytes)('dcrypt-recipient-v1'), FILE_KEY_LENGTH);
|