@forthtilliath/backup-crypto 0.1.0 → 0.1.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 CHANGED
@@ -69,6 +69,28 @@ A keyring keeps every key an app ever encrypted with, so files written before a
69
69
  | `sameKeyring(a, b)` | Nothing to save or upload |
70
70
  | `serializeKeyring` / `parseKeyring` | JSON form (contains the raw keys — store it securely) |
71
71
 
72
+ ### API reference
73
+
74
+ All functions are pure (no I/O); the keyring helpers return new objects.
75
+
76
+ | Export | Module | |
77
+ | ----------------------------------------------------------------------- | -------------- | --------------------------------------------------------------------------------------------- |
78
+ | `generateBackupKey(randomBytes?)` | `backupKey` | A new random 256-bit key (`BACKUP_KEY_LENGTH` = 32 bytes) |
79
+ | `getBackupKeyId(key)` | `backupKey` | Short non-secret id (16 hex chars) of a key, stored in each envelope |
80
+ | `encodeBackupKey(key)` / `decodeBackupKey(text)` | `backupKey` | Base64 form of a key, for storage; decoding throws `corrupted` for anything but a 256-bit key |
81
+ | `defaultRandomBytes(length)` | `backupKey` | `crypto.getRandomValues`, or throws `no-random-source` |
82
+ | `encryptBackup(text, key, { app?, randomBytes? })` | `envelope` | The JSON envelope to write as is |
83
+ | `decryptBackup(content, key)` | `envelope` | Plaintext; throws `unknown-key` or `corrupted` |
84
+ | `isEncryptedBackup(content)` | `envelope` | Whether a file is an envelope (of any version) |
85
+ | `readEncryptedBackup(content)` | `envelope` | Validated header (`app`, `keyId`, `version`…) without decrypting |
86
+ | `ENCRYPTED_BACKUP_FORMAT` / `ENCRYPTED_BACKUP_VERSION` | `envelope` | `format` field of an envelope / version this package writes |
87
+ | `emptyKeyring`, `getCurrentKey(keyring)`, `findKeyringKey(keyring, id)` | `keyring` | Keyring basics |
88
+ | `decryptBackupWithKeyring(content, keyring)` | `keyring` | Decrypts with whichever key wrote the file |
89
+ | `BACKUP_KEYRING_FORMAT` | `keyring` | `format` field of a serialized keyring |
90
+ | `formatRecoveryCode(key)` / `parseRecoveryCode(code)` | `recoveryCode` | Human-friendly form of a key, with a checksum |
91
+
92
+ Everything is exported from the package root; each module is also importable on its own (`@forthtilliath/backup-crypto/keyring`…).
93
+
72
94
  ### Errors
73
95
 
74
96
  Everything throws a `BackupCryptoError` with a `code`: `not-encrypted`, `unsupported-version`, `unknown-key` (ask for the recovery code of the device that wrote it), `corrupted` (truncated or tampered with), `invalid-recovery-code` (typo), `no-random-source`.
@@ -8,7 +8,14 @@ export type RandomBytes = (length: number) => Uint8Array;
8
8
  * polyfill). Throws rather than ever falling back to `Math.random`.
9
9
  */
10
10
  export declare function defaultRandomBytes(length: number): Uint8Array;
11
- /** Generates a new random 256-bit backup key. */
11
+ /**
12
+ * Generates a new random 256-bit backup key.
13
+ *
14
+ * @param randomBytes - Source of randomness. Defaults to
15
+ * `crypto.getRandomValues`; pass
16
+ * expo-crypto's `getRandomBytes` where the global is missing.
17
+ * @returns The 32-byte key. Keep it secret (secure store, private cloud file).
18
+ */
12
19
  export declare function generateBackupKey(randomBytes?: RandomBytes): Uint8Array;
13
20
  /**
14
21
  * A short, stable, non-secret identifier of a key (16 hex chars), stored next
@@ -18,6 +25,9 @@ export declare function generateBackupKey(randomBytes?: RandomBytes): Uint8Array
18
25
  export declare function getBackupKeyId(key: Uint8Array): string;
19
26
  /** Base64 form of a key, for storage (secure store, keyring file). */
20
27
  export declare function encodeBackupKey(key: Uint8Array): string;
21
- /** Reverse of {@link encodeBackupKey}. Throws `corrupted` on anything else than a 256-bit key. */
28
+ /**
29
+ * Reverse of {@link encodeBackupKey}. Throws `corrupted` on anything else than
30
+ * a 256-bit key.
31
+ */
22
32
  export declare function decodeBackupKey(encoded: string): Uint8Array;
23
33
  //# sourceMappingURL=backupKey.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"backupKey.d.ts","sourceRoot":"","sources":["../src/backupKey.ts"],"names":[],"mappings":"AAMA,8EAA8E;AAC9E,eAAO,MAAM,iBAAiB,KAAK,CAAC;AAEpC,6EAA6E;AAC7E,MAAM,MAAM,WAAW,GAAG,CAAC,MAAM,EAAE,MAAM,KAAK,UAAU,CAAC;AAIzD;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAS7D;AAED,iDAAiD;AACjD,wBAAgB,iBAAiB,CAC/B,WAAW,GAAE,WAAgC,GAC5C,UAAU,CASZ;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,UAAU,GAAG,MAAM,CAKtD;AAED,sEAAsE;AACtE,wBAAgB,eAAe,CAAC,GAAG,EAAE,UAAU,GAAG,MAAM,CAEvD;AAED,kGAAkG;AAClG,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU,CAW3D"}
1
+ {"version":3,"file":"backupKey.d.ts","sourceRoot":"","sources":["../src/backupKey.ts"],"names":[],"mappings":"AAMA,8EAA8E;AAC9E,eAAO,MAAM,iBAAiB,KAAK,CAAC;AAEpC,6EAA6E;AAC7E,MAAM,MAAM,WAAW,GAAG,CAAC,MAAM,EAAE,MAAM,KAAK,UAAU,CAAC;AAIzD;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAS7D;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAC/B,WAAW,GAAE,WAAgC,GAC5C,UAAU,CASZ;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,UAAU,GAAG,MAAM,CAKtD;AAED,sEAAsE;AACtE,wBAAgB,eAAe,CAAC,GAAG,EAAE,UAAU,GAAG,MAAM,CAEvD;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU,CAW3D"}
package/dist/backupKey.js CHANGED
@@ -17,7 +17,14 @@ export function defaultRandomBytes(length) {
17
17
  }
18
18
  return webCrypto.getRandomValues(new Uint8Array(length));
19
19
  }
20
- /** Generates a new random 256-bit backup key. */
20
+ /**
21
+ * Generates a new random 256-bit backup key.
22
+ *
23
+ * @param randomBytes - Source of randomness. Defaults to
24
+ * `crypto.getRandomValues`; pass
25
+ * expo-crypto's `getRandomBytes` where the global is missing.
26
+ * @returns The 32-byte key. Keep it secret (secure store, private cloud file).
27
+ */
21
28
  export function generateBackupKey(randomBytes = defaultRandomBytes) {
22
29
  const key = randomBytes(BACKUP_KEY_LENGTH);
23
30
  if (key.length !== BACKUP_KEY_LENGTH) {
@@ -40,7 +47,10 @@ export function getBackupKeyId(key) {
40
47
  export function encodeBackupKey(key) {
41
48
  return base64.encode(key);
42
49
  }
43
- /** Reverse of {@link encodeBackupKey}. Throws `corrupted` on anything else than a 256-bit key. */
50
+ /**
51
+ * Reverse of {@link encodeBackupKey}. Throws `corrupted` on anything else than
52
+ * a 256-bit key.
53
+ */
44
54
  export function decodeBackupKey(encoded) {
45
55
  let key;
46
56
  try {
@@ -1,5 +1,13 @@
1
1
  import { type RandomBytes } from "./backupKey.js";
2
+ /**
3
+ * Value of the `format` field that marks a file as an encrypted backup
4
+ * envelope.
5
+ */
2
6
  export declare const ENCRYPTED_BACKUP_FORMAT = "forthtilliath.encrypted-backup";
7
+ /**
8
+ * Envelope version this package writes; a higher one is refused as
9
+ * `unsupported-version`.
10
+ */
3
11
  export declare const ENCRYPTED_BACKUP_VERSION = 1;
4
12
  declare const CIPHER = "xchacha20-poly1305";
5
13
  /**
@@ -8,19 +16,36 @@ declare const CIPHER = "xchacha20-poly1305";
8
16
  * app's data is in `ciphertext`.
9
17
  */
10
18
  export interface EncryptedBackupEnvelope {
19
+ /** Always {@link ENCRYPTED_BACKUP_FORMAT}. */
11
20
  format: typeof ENCRYPTED_BACKUP_FORMAT;
21
+ /**
22
+ * Envelope version, {@link ENCRYPTED_BACKUP_VERSION} when written by this
23
+ * package.
24
+ */
12
25
  version: number;
13
26
  /** Free label of the app that wrote it — authenticated, not secret. */
14
27
  app: string;
15
28
  /** {@link getBackupKeyId} of the key it was encrypted with. */
16
29
  keyId: string;
30
+ /** The algorithm, always `"xchacha20-poly1305"`. */
17
31
  cipher: typeof CIPHER;
32
+ /** The random 24-byte nonce, base64-encoded. */
18
33
  nonce: string;
34
+ /**
35
+ * The encrypted backup followed by its 16-byte authentication tag,
36
+ * base64-encoded.
37
+ */
19
38
  ciphertext: string;
20
39
  }
40
+ /** Options of {@link encryptBackup}. */
21
41
  export interface EncryptBackupOptions {
22
42
  /** Stored in clear and authenticated, e.g. `"glucodose"`. */
23
43
  app?: string;
44
+ /**
45
+ * Source of the nonce. Defaults to the platform's secure generator
46
+ * (`crypto.getRandomValues`); override it only in tests, for a deterministic
47
+ * output.
48
+ */
24
49
  randomBytes?: RandomBytes;
25
50
  }
26
51
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"envelope.d.ts","sourceRoot":"","sources":["../src/envelope.ts"],"names":[],"mappings":"AAIA,OAAO,EAIL,KAAK,WAAW,EACjB,MAAM,gBAAgB,CAAC;AAGxB,eAAO,MAAM,uBAAuB,mCAAmC,CAAC;AACxE,eAAO,MAAM,wBAAwB,IAAI,CAAC;AAC1C,QAAA,MAAM,MAAM,uBAAuB,CAAC;AAGpC;;;;GAIG;AACH,MAAM,WAAW,uBAAuB;IACtC,MAAM,EAAE,OAAO,uBAAuB,CAAC;IACvC,OAAO,EAAE,MAAM,CAAC;IAChB,uEAAuE;IACvE,GAAG,EAAE,MAAM,CAAC;IACZ,+DAA+D;IAC/D,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,OAAO,MAAM,CAAC;IACtB,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,oBAAoB;IACnC,6DAA6D;IAC7D,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B;AA2BD;;;;GAIG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,MAAM,EACjB,GAAG,EAAE,UAAU,EACf,EAAE,G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AAgC,EAAE,GAAE,oBAAyB,GACxE,MAAM,CAwBR;AAcD,0EAA0E;AAC1E,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAG1D;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,GAAG,uBAAuB,CA8B5E;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,GAAG,MAAM,CAqBtE"}
package/dist/envelope.js CHANGED
@@ -3,7 +3,15 @@ import { bytesToUtf8, utf8ToBytes } from "@noble/ciphers/utils.js";
3
3
  import { base64 } from "@scure/base";
4
4
  import { BACKUP_KEY_LENGTH, defaultRandomBytes, getBackupKeyId, } from "./backupKey.js";
5
5
  import { BackupCryptoError } from "./errors.js";
6
+ /**
7
+ * Value of the `format` field that marks a file as an encrypted backup
8
+ * envelope.
9
+ */
6
10
  export const ENCRYPTED_BACKUP_FORMAT = "forthtilliath.encrypted-backup";
11
+ /**
12
+ * Envelope version this package writes; a higher one is refused as
13
+ * `unsupported-version`.
14
+ */
7
15
  export const ENCRYPTED_BACKUP_VERSION = 1;
8
16
  const CIPHER = "xchacha20-poly1305";
9
17
  const NONCE_LENGTH = 24;
package/dist/errors.d.ts CHANGED
@@ -11,6 +11,11 @@
11
11
  * option — never encrypt with a weak random source instead.
12
12
  */
13
13
  export type BackupCryptoErrorCode = "not-encrypted" | "unsupported-version" | "unknown-key" | "corrupted" | "invalid-recovery-code" | "no-random-source";
14
+ /**
15
+ * The only error this package throws: `code` says what went wrong (see {@link
16
+ * BackupCryptoErrorCode}), so the app can show the right message — e.g. ask for
17
+ * the recovery key on `unknown-key`.
18
+ */
14
19
  export declare class BackupCryptoError extends Error {
15
20
  readonly code: BackupCryptoErrorCode;
16
21
  constructor(code: BackupCryptoErrorCode, message?: string);
@@ -1 +1 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,qBAAqB,GAC7B,eAAe,GACf,qBAAqB,GACrB,aAAa,GACb,WAAW,GACX,uBAAuB,GACvB,kBAAkB,CAAC;AAEvB,qBAAa,iBAAkB,SAAQ,KAAK;IAC1C,QAAQ,CAAC,IAAI,EAAE,qBAAqB,CAAC;gBAEzB,IAAI,EAAE,qBAAqB,EAAE,OAAO,CAAC,EAAE,MAAM;CAK1D"}
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,qBAAqB,GAC7B,eAAe,GACf,qBAAqB,GACrB,aAAa,GACb,WAAW,GACX,uBAAuB,GACvB,kBAAkB,CAAC;AAEvB;;;;GAIG;AACH,qBAAa,iBAAkB,SAAQ,KAAK;IAC1C,QAAQ,CAAC,IAAI,EAAE,qBAAqB,CAAC;gBAEzB,IAAI,EAAE,qBAAqB,EAAE,OAAO,CAAC,EAAE,MAAM;CAK1D"}
package/dist/errors.js CHANGED
@@ -1,3 +1,8 @@
1
+ /**
2
+ * The only error this package throws: `code` says what went wrong (see {@link
3
+ * BackupCryptoErrorCode}), so the app can show the right message — e.g. ask for
4
+ * the recovery key on `unknown-key`.
5
+ */
1
6
  export class BackupCryptoError extends Error {
2
7
  code;
3
8
  constructor(code, message) {
package/dist/keyring.d.ts CHANGED
@@ -1,7 +1,20 @@
1
1
  import { type RandomBytes } from "./backupKey.js";
2
+ /**
3
+ * Value of the `format` field of a serialized keyring (see {@link
4
+ * serializeKeyring}).
5
+ */
2
6
  export declare const BACKUP_KEYRING_FORMAT = "forthtilliath.backup-keyring";
7
+ /** One key of a keyring. */
3
8
  export interface BackupKeyringEntry {
9
+ /**
10
+ * {@link getBackupKeyId} of `key`: short, non-secret, stored in every
11
+ * envelope it encrypts.
12
+ */
4
13
  id: string;
14
+ /**
15
+ * The raw 32-byte key. Secret: keep it in a secure store or a private cloud
16
+ * file.
17
+ */
5
18
  key: Uint8Array;
6
19
  /** Epoch ms. */
7
20
  createdAt: number;
@@ -12,11 +25,33 @@ export interface BackupKeyringEntry {
12
25
  * use `currentId`. Immutable: every helper returns a new keyring.
13
26
  */
14
27
  export interface BackupKeyring {
28
+ /**
29
+ * Id of the key new backups are encrypted with, `null` while the keyring is
30
+ * empty.
31
+ */
15
32
  currentId: string | null;
33
+ /**
34
+ * Every key known on this device — older ones are kept to decrypt older
35
+ * backups.
36
+ */
16
37
  keys: readonly BackupKeyringEntry[];
17
38
  }
39
+ /**
40
+ * A keyring with no key, the starting point before {@link ensureCurrentKey}.
41
+ */
18
42
  export declare const emptyKeyring: BackupKeyring;
43
+ /**
44
+ * Looks a key up by id.
45
+ * @param keyring - The keyring to search.
46
+ * @param id - A key id, e.g. an envelope's `keyId`.
47
+ * @returns The entry, or `undefined` when this keyring doesn't have it.
48
+ */
19
49
  export declare function findKeyringKey(keyring: BackupKeyring, id: string): BackupKeyringEntry | undefined;
50
+ /**
51
+ * Returns the key new backups should be encrypted with.
52
+ * @param keyring - The keyring.
53
+ * @returns The current entry, or `undefined` when the keyring has none.
54
+ */
20
55
  export declare function getCurrentKey(keyring: BackupKeyring): BackupKeyringEntry | undefined;
21
56
  /**
22
57
  * Adds `key` (a no-op if already there). It becomes the current key when
@@ -40,7 +75,9 @@ export declare function sameKeyring(a: BackupKeyring, b: BackupKeyring): boolean
40
75
  * Throws `unknown-key` if none matches.
41
76
  */
42
77
  export declare function decryptBackupWithKeyring(content: string, keyring: BackupKeyring): string;
43
- /** JSON form, for a secure store or a private cloud file. Contains the raw keys. */
78
+ /**
79
+ * JSON form, for a secure store or a private cloud file. Contains the raw keys.
80
+ */
44
81
  export declare function serializeKeyring(keyring: BackupKeyring): string;
45
82
  /**
46
83
  * Reverse of {@link serializeKeyring}. Key ids are recomputed from the keys
@@ -1 +1 @@
1
- {"version":3,"file":"keyring.d.ts","sourceRoot":"","sources":["../src/keyring.ts"],"names":[],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9E,wBAAgB,WAAW,CAAC,CAAC,EAAE,aAAa,EAAE,CAAC,EAAE,aAAa,GAAG,OAAO,CAMvE;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CACtC,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,aAAa,GACrB,MAAM,CAKR;AASD;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,aAAa,GAAG,MAAM,CAW/D;AAMD;;;GAGG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,aAAa,CAmC3D"}
package/dist/keyring.js CHANGED
@@ -1,12 +1,30 @@
1
1
  import { decodeBackupKey, defaultRandomBytes, encodeBackupKey, generateBackupKey, getBackupKeyId, } from "./backupKey.js";
2
2
  import { decryptBackup, readEncryptedBackup } from "./envelope.js";
3
3
  import { BackupCryptoError } from "./errors.js";
4
+ /**
5
+ * Value of the `format` field of a serialized keyring (see {@link
6
+ * serializeKeyring}).
7
+ */
4
8
  export const BACKUP_KEYRING_FORMAT = "forthtilliath.backup-keyring";
5
9
  const BACKUP_KEYRING_VERSION = 1;
10
+ /**
11
+ * A keyring with no key, the starting point before {@link ensureCurrentKey}.
12
+ */
6
13
  export const emptyKeyring = { currentId: null, keys: [] };
14
+ /**
15
+ * Looks a key up by id.
16
+ * @param keyring - The keyring to search.
17
+ * @param id - A key id, e.g. an envelope's `keyId`.
18
+ * @returns The entry, or `undefined` when this keyring doesn't have it.
19
+ */
7
20
  export function findKeyringKey(keyring, id) {
8
21
  return keyring.keys.find((entry) => entry.id === id);
9
22
  }
23
+ /**
24
+ * Returns the key new backups should be encrypted with.
25
+ * @param keyring - The keyring.
26
+ * @returns The current entry, or `undefined` when the keyring has none.
27
+ */
10
28
  export function getCurrentKey(keyring) {
11
29
  return keyring.currentId === null
12
30
  ? undefined
@@ -61,7 +79,9 @@ export function decryptBackupWithKeyring(content, keyring) {
61
79
  throw new BackupCryptoError("unknown-key");
62
80
  return decryptBackup(content, entry.key);
63
81
  }
64
- /** JSON form, for a secure store or a private cloud file. Contains the raw keys. */
82
+ /**
83
+ * JSON form, for a secure store or a private cloud file. Contains the raw keys.
84
+ */
65
85
  export function serializeKeyring(keyring) {
66
86
  const serialized = {
67
87
  format: BACKUP_KEYRING_FORMAT,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forthtilliath/backup-crypto",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "license": "MIT",
@@ -27,11 +27,11 @@
27
27
  "@scure/base": "^2.4.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@vitest/coverage-v8": "5.0.2",
31
- "eslint": "10.7.0",
30
+ "@vitest/coverage-v8": "5.0.5",
31
+ "eslint": "10.14.0",
32
32
  "typescript": "^5.9.3",
33
- "vitest": "^5.0.2",
34
- "@forthtilliath/eslint-config": "0.9.0",
33
+ "vitest": "^5.0.5",
34
+ "@forthtilliath/eslint-config": "0.10.2",
35
35
  "@forthtilliath/typescript-config": "0.1.1"
36
36
  },
37
37
  "scripts": {