All files / agecrypt.ts

100.00% Branches 8/8
100.00% Functions 5/5
100.00% Lines 55/55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
x5
x5
x5
x5
x5
x5
x5
 
 
 
x5
x5
 
 
x5
x5
x4
x4
 
 
x5
x5
x5
 
x5
x2
x2
x2
x2
 
x5
x2
x2
x2
 
 
x2
x2
x2
x2
x2
x2
 
x2
 
x2
x2
x2
 
x2
x2
x2
 
x2
x2
x5
 
x5
x5
x5
x5
 
x6
x6
x3
x3
x2
x2
x1
x1
x4
x6






































































import { armor, Encrypter, type Recipient, Stanza } from "age-encryption";
import { ed25519, x25519 } from "@noble/curves/ed25519.js";
import { hkdf } from "@noble/hashes/hkdf.js";
import { sha256 } from "@noble/hashes/sha2.js";
import { chacha20poly1305 } from "@noble/ciphers/chacha.js";
import { encodeBase64 } from "@std/encoding/base64";
import { parseSshEd25519 } from "./sshkey.ts";

// age's ssh-ed25519 recipient stanza (see ADR-0002). typage has no SSH-recipient
// support, so we reimplement the stanza on the same primitives typage uses.
const SSH_ED25519_LABEL = new TextEncoder().encode(
  "age-encryption.org/v1/ssh-ed25519",
);

/** Standard base64, no padding — the encoding age uses for stanza arguments. */
function b64(bytes: Uint8Array): string {
  return encodeBase64(bytes).replace(/=+$/, "");
}

/** A typage Recipient that wraps the file key for an `ssh-ed25519` key. */
class SshEd25519Recipient implements Recipient {
  readonly #wire: Uint8Array;
  readonly #montgomery: Uint8Array;

  constructor(sshKey: string) {
    const parsed = parseSshEd25519(sshKey);
    this.#wire = parsed.wire;
    this.#montgomery = ed25519.utils.toMontgomery(parsed.ed25519);
  }

  wrapFileKey(fileKey: Uint8Array): Stanza[] {
    const ephemeralSecret = x25519.utils.randomSecretKey();
    const share = x25519.getPublicKey(ephemeralSecret);
    let shared = x25519.getSharedSecret(ephemeralSecret, this.#montgomery);

    // age tweaks the shared secret by a scalar derived from the ssh key.
    const tweak = hkdf(
      sha256,
      new Uint8Array(0),
      this.#wire,
      SSH_ED25519_LABEL,
      32,
    );
    shared = x25519.getSharedSecret(tweak, shared);

    const salt = new Uint8Array(share.length + this.#montgomery.length);
    salt.set(share, 0);
    salt.set(this.#montgomery, share.length);

    const wrapKey = hkdf(sha256, shared, salt, SSH_ED25519_LABEL, 32);
    const body = chacha20poly1305(wrapKey, new Uint8Array(12)).encrypt(fileKey);
    const tag = b64(sha256(this.#wire).subarray(0, 4));

    return [new Stanza(["ssh-ed25519", tag, b64(share)], body)];
  }
}

/** Encrypt `plaintext` to a recipient (native `age1…` or an `ssh-ed25519` key); returns armored age. */
export async function encryptToRecipient(
  recipient: string,
  plaintext: Uint8Array,
): Promise<string> {
  const encrypter = new Encrypter();
  if (recipient.startsWith("age1")) {
    encrypter.addRecipient(recipient);
  } else if (recipient.startsWith("ssh-ed25519 ")) {
    encrypter.addRecipient(new SshEd25519Recipient(recipient));
  } else {
    throw new Error("unsupported recipient");
  }
  return armor.encode(await encrypter.encrypt(plaintext));
}