@interop/minimal-cipher 7.0.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.
Files changed (83) hide show
  1. package/LICENSE +27 -0
  2. package/README.md +220 -0
  3. package/dist/Cipher.d.ts +210 -0
  4. package/dist/Cipher.d.ts.map +1 -0
  5. package/dist/Cipher.js +313 -0
  6. package/dist/Cipher.js.map +1 -0
  7. package/dist/DecryptTransformer.d.ts +14 -0
  8. package/dist/DecryptTransformer.d.ts.map +1 -0
  9. package/dist/DecryptTransformer.js +125 -0
  10. package/dist/DecryptTransformer.js.map +1 -0
  11. package/dist/EncryptTransformer.d.ts +28 -0
  12. package/dist/EncryptTransformer.d.ts.map +1 -0
  13. package/dist/EncryptTransformer.js +94 -0
  14. package/dist/EncryptTransformer.js.map +1 -0
  15. package/dist/algorithms/a256gcm.d.ts +39 -0
  16. package/dist/algorithms/a256gcm.d.ts.map +1 -0
  17. package/dist/algorithms/a256gcm.js +101 -0
  18. package/dist/algorithms/a256gcm.js.map +1 -0
  19. package/dist/algorithms/aeskw.d.ts +5 -0
  20. package/dist/algorithms/aeskw.d.ts.map +1 -0
  21. package/dist/algorithms/aeskw.js +68 -0
  22. package/dist/algorithms/aeskw.js.map +1 -0
  23. package/dist/algorithms/c20p-browser.d.ts +51 -0
  24. package/dist/algorithms/c20p-browser.d.ts.map +1 -0
  25. package/dist/algorithms/c20p-browser.js +106 -0
  26. package/dist/algorithms/c20p-browser.js.map +1 -0
  27. package/dist/algorithms/c20p.d.ts +51 -0
  28. package/dist/algorithms/c20p.d.ts.map +1 -0
  29. package/dist/algorithms/c20p.js +110 -0
  30. package/dist/algorithms/c20p.js.map +1 -0
  31. package/dist/algorithms/ecdhkdf.d.ts +24 -0
  32. package/dist/algorithms/ecdhkdf.d.ts.map +1 -0
  33. package/dist/algorithms/ecdhkdf.js +76 -0
  34. package/dist/algorithms/ecdhkdf.js.map +1 -0
  35. package/dist/algorithms/fips.d.ts +8 -0
  36. package/dist/algorithms/fips.d.ts.map +1 -0
  37. package/dist/algorithms/fips.js +8 -0
  38. package/dist/algorithms/fips.js.map +1 -0
  39. package/dist/algorithms/p256.d.ts +24 -0
  40. package/dist/algorithms/p256.d.ts.map +1 -0
  41. package/dist/algorithms/p256.js +106 -0
  42. package/dist/algorithms/p256.js.map +1 -0
  43. package/dist/algorithms/recommended.d.ts +8 -0
  44. package/dist/algorithms/recommended.d.ts.map +1 -0
  45. package/dist/algorithms/recommended.js +8 -0
  46. package/dist/algorithms/recommended.js.map +1 -0
  47. package/dist/algorithms/x25519-helper-browser.d.ts +7 -0
  48. package/dist/algorithms/x25519-helper-browser.d.ts.map +1 -0
  49. package/dist/algorithms/x25519-helper-browser.js +25 -0
  50. package/dist/algorithms/x25519-helper-browser.js.map +1 -0
  51. package/dist/algorithms/x25519-helper.d.ts +7 -0
  52. package/dist/algorithms/x25519-helper.d.ts.map +1 -0
  53. package/dist/algorithms/x25519-helper.js +51 -0
  54. package/dist/algorithms/x25519-helper.js.map +1 -0
  55. package/dist/algorithms/x25519.d.ts +44 -0
  56. package/dist/algorithms/x25519.d.ts.map +1 -0
  57. package/dist/algorithms/x25519.js +112 -0
  58. package/dist/algorithms/x25519.js.map +1 -0
  59. package/dist/algorithms/xc20p.d.ts +40 -0
  60. package/dist/algorithms/xc20p.d.ts.map +1 -0
  61. package/dist/algorithms/xc20p.js +160 -0
  62. package/dist/algorithms/xc20p.js.map +1 -0
  63. package/dist/baseX.d.ts +7 -0
  64. package/dist/baseX.d.ts.map +1 -0
  65. package/dist/baseX.js +11 -0
  66. package/dist/baseX.js.map +1 -0
  67. package/dist/crypto.d.ts +3 -0
  68. package/dist/crypto.d.ts.map +1 -0
  69. package/dist/crypto.js +3 -0
  70. package/dist/crypto.js.map +1 -0
  71. package/dist/index.d.ts +5 -0
  72. package/dist/index.d.ts.map +1 -0
  73. package/dist/index.js +5 -0
  74. package/dist/index.js.map +1 -0
  75. package/dist/types.d.ts +119 -0
  76. package/dist/types.d.ts.map +1 -0
  77. package/dist/types.js +5 -0
  78. package/dist/types.js.map +1 -0
  79. package/dist/util.d.ts +2 -0
  80. package/dist/util.d.ts.map +1 -0
  81. package/dist/util.js +11 -0
  82. package/dist/util.js.map +1 -0
  83. package/package.json +99 -0
@@ -0,0 +1,160 @@
1
+ /*!
2
+ * Copyright (c) 2019-2023 Digital Bazaar, Inc. All rights reserved.
3
+ */
4
+ import { _chacha20, decrypt as _decrypt, _encrypt } from './c20p.js';
5
+ import crypto from '../crypto.js';
6
+ // constants are based on the string: "expand 32-byte k"
7
+ const CHACHA20_CONSTANTS = [
8
+ 0x61707865, // "expa" referred to as the "sigma" constant
9
+ 0x3320646e, // "nd 3" keys used here must be 32-bytes
10
+ 0x79622d32, // "2-by"
11
+ 0x6b206574 // "te k"
12
+ ];
13
+ const LE = true;
14
+ const NULL_DATA = new Uint8Array(64);
15
+ export const JWE_ENC = 'XC20P';
16
+ /**
17
+ * Generates a content encryption key (CEK). The 256-bit key is intended to be
18
+ * used as a XChaCha20Poly1305 (draft-irtf-cfrg-xchacha-01) key.
19
+ *
20
+ * @returns {Promise<Uint8Array>} - Resolves to the generated key.
21
+ */
22
+ export async function generateKey() {
23
+ // generate content encryption key
24
+ return crypto.getRandomValues(new Uint8Array(32));
25
+ }
26
+ /**
27
+ * Encrypts some data. The data will be encrypted using the given
28
+ * 256-bit XChaCha20Poly1305 (draft-irtf-cfrg-xchacha-01) content encryption
29
+ * key (CEK).
30
+ *
31
+ * @param {object} options - The options to use.
32
+ * @param {Uint8Array} options.data - The data to encrypt.
33
+ * @param {Uint8Array} [options.additionalData] - Optional additional
34
+ * authentication data.
35
+ * @param {Uint8Array} options.cek - The content encryption key to use.
36
+ *
37
+ * @returns {Promise<object>} - Resolves to `{ciphertext, iv, tag}`.
38
+ */
39
+ export async function encrypt({ data, additionalData, cek }) {
40
+ if (!(data instanceof Uint8Array)) {
41
+ throw new TypeError('"data" must be a Uint8Array.');
42
+ }
43
+ if (!(cek instanceof Uint8Array)) {
44
+ throw new TypeError('"cek" must be a Uint8Array.');
45
+ }
46
+ // generate 24-byte (192-bit) XChaCha20Poly1305 IV and use it and `cek` to
47
+ // generate subkey and 12-byte (96-bit) IV for use with ChaCha20Poly1305
48
+ const nonce = crypto.getRandomValues(new Uint8Array(24));
49
+ const { subkey, iv } = await _generateSubkey({ cek, nonce });
50
+ // run ChaCha20Poly1305
51
+ const result = await _encrypt({ data, additionalData, cek: subkey, iv });
52
+ // return full XChaCha20Poly1305 nonce as IV
53
+ result.iv = nonce;
54
+ // wipe generated values
55
+ subkey.fill(0);
56
+ iv.fill(0);
57
+ return result;
58
+ }
59
+ /**
60
+ * Decrypts some encrypted data. The data must have been encrypted using
61
+ * the given XChaCha20Poly1305 (draft-irtf-cfrg-xchacha-01) content encryption
62
+ * key (CEK).
63
+ *
64
+ * @param {object} options - The options to use.
65
+ * @param {Uint8Array} options.ciphertext - The data to decrypt.
66
+ * @param {Uint8Array} options.iv - The initialization vector (aka nonce).
67
+ * @param {Uint8Array} options.tag - The authentication tag.
68
+ * @param {Uint8Array} [options.additionalData] - Optional additional
69
+ * authentication data.
70
+ * @param {Uint8Array} options.cek - The content encryption key to use.
71
+ *
72
+ * @returns {Promise<Uint8Array>} The decrypted data.
73
+ */
74
+ export async function decrypt({ ciphertext, iv, tag, additionalData, cek }) {
75
+ if (!(iv instanceof Uint8Array)) {
76
+ throw new Error('Invalid or missing "iv".');
77
+ }
78
+ if (!(ciphertext instanceof Uint8Array)) {
79
+ throw new Error('Invalid or missing "ciphertext".');
80
+ }
81
+ if (!(tag instanceof Uint8Array)) {
82
+ throw new Error('Invalid or missing "tag".');
83
+ }
84
+ if (!(cek instanceof Uint8Array)) {
85
+ throw new TypeError('"cek" must be a Uint8Array.');
86
+ }
87
+ // generate subkey and 12-byte (96-bit) IV for use with ChaCha20Poly1305 from
88
+ // `cek` and 24-byte (192-bit) XChaCha20Poly1305 IV
89
+ const { subkey, iv: newIV } = await _generateSubkey({ cek, nonce: iv });
90
+ // decrypt `ciphertext`
91
+ const result = await _decrypt({
92
+ ciphertext,
93
+ iv: newIV,
94
+ tag,
95
+ additionalData,
96
+ cek: subkey
97
+ });
98
+ // wipe generated values
99
+ subkey.fill(0);
100
+ newIV.fill(0);
101
+ return result;
102
+ }
103
+ async function _generateSubkey({ cek, nonce }) {
104
+ // generate subkey and 12-byte IV for ChaCha20Poly1305; first 4 bytes of
105
+ // IV are NULL bytes, last 8 are the last 8 bytes of the randomly generated
106
+ // 24-byte XChaCha20Poly1305 nonce
107
+ const subkey = await _hchacha20({ key: cek, nonce: nonce.subarray(0, 16) });
108
+ const iv = new Uint8Array(12);
109
+ iv.set(nonce.subarray(16), 4);
110
+ return { subkey, iv };
111
+ }
112
+ async function _hchacha20({ key, nonce }) {
113
+ /* HChaCha20's output is the first 16 bytes of internal state and last 16
114
+ bytes of ChaCha20 internal state after running its usual rounds. See:
115
+
116
+ https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha#section-2.2
117
+
118
+ ChaCha20's output is each 4 bytes of internal state (interpreted as a LE
119
+ uint32) after running the usual rounds added to the initial internal state
120
+ (again, with each 4 bytes interpreted as a LE uint32).
121
+
122
+ Therefore, we can implement HChaCha20 by:
123
+
124
+ 1. Creating the ChaCha20 initial state by concatenating the ChaCha20
125
+ constants with the 32-byte key and the 16-byte nonce.
126
+ 2. Running ChaCha20 with the 32-byte key, 16-byte nonce, and zero-filled
127
+ 64-byte data to get 64 byte output. The zero-filled data makes the
128
+ internal XOR operations no-ops.
129
+ 3. Read the first 16 bytes and last 16 bytes of ChaCha20 output as LE uint32s
130
+ and subtract each corresponding LE uint32 from the initial state.
131
+ 4. Concatenate the resulting 32 bytes to produce the HChaCha20 output. */
132
+ // create initial ChaCha20 state as 16 LE uint32s (no need to convert to
133
+ // 64 bytes as the numbers will be used directly below)
134
+ const state = new Array(16);
135
+ for (let i = 0; i < 4; ++i) {
136
+ state[i] = CHACHA20_CONSTANTS[i];
137
+ }
138
+ const dvKey = new DataView(key.buffer, key.byteOffset, key.length);
139
+ for (let i = 0; i < 8; ++i) {
140
+ state[i + 4] = dvKey.getUint32(i * 4, LE);
141
+ }
142
+ const dvNonce = new DataView(nonce.buffer, nonce.byteOffset, nonce.length);
143
+ for (let i = 0; i < 4; ++i) {
144
+ state[i + 12] = dvNonce.getUint32(i * 4, LE);
145
+ }
146
+ // run ChaCha20
147
+ const dst = await _chacha20({ key, nonce, src: NULL_DATA });
148
+ // generate HChaCha20 output
149
+ const out = new Uint8Array(32);
150
+ const dvOut = new DataView(out.buffer, out.byteOffset, out.length);
151
+ const dvDst = new DataView(dst.buffer, dst.byteOffset, dst.length);
152
+ for (let i = 0; i < 4; ++i) {
153
+ dvOut.setUint32(i * 4, (dvDst.getUint32(i * 4, LE) - state[i]) | 0, LE);
154
+ }
155
+ for (let i = 0; i < 4; ++i) {
156
+ dvOut.setUint32(i * 4 + 16, (dvDst.getUint32(i * 4 + 48, LE) - state[i + 12]) | 0, LE);
157
+ }
158
+ return out;
159
+ }
160
+ //# sourceMappingURL=xc20p.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xc20p.js","sourceRoot":"","sources":["../../src/algorithms/xc20p.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,SAAS,EAAE,OAAO,IAAI,QAAQ,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AACpE,OAAO,MAAM,MAAM,cAAc,CAAA;AAOjC,wDAAwD;AACxD,MAAM,kBAAkB,GAAG;IACzB,UAAU,EAAE,6CAA6C;IACzD,UAAU,EAAE,yCAAyC;IACrD,UAAU,EAAE,SAAS;IACrB,UAAU,CAAC,SAAS;CACrB,CAAA;AACD,MAAM,EAAE,GAAG,IAAI,CAAA;AACf,MAAM,SAAS,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAA;AAEpC,MAAM,CAAC,MAAM,OAAO,GAAG,OAAO,CAAA;AAE9B;;;;;GAKG;AACH,MAAM,CAAC,KAAK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AAE,GAAG,EAAE,SAAS,EAAE,CAAC,CAAA;IAE3D,4BAA4B;IAC5B,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAA;IAC9B,MAAM,KAAK,GAAG,IAAI,QAAQ,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IAClE,MAAM,KAAK,GAAG,IAAI,QAAQ,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IAClE,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC;QAC3B,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,CAAA;IACzE,CAAC;IACD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC;QAC3B,KAAK,CAAC,SAAS,CACb,CAAC,GAAG,CAAC,GAAG,EAAE,EACV,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,EAAE,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,CAAC,EACrD,EAAE,CACH,CAAA;IACH,CAAC;IAED,OAAO,GAAG,CAAA;AACZ,CAAC"}
@@ -0,0 +1,7 @@
1
+ export declare const base58btc: import("@scure/base").BytesCoder;
2
+ /**
3
+ * base64url must be RFC 4648 compliant for JWK interop, and JWK uses the
4
+ * unpadded form, hence `base64urlnopad`.
5
+ */
6
+ export declare const base64url: import("@scure/base").BytesCoder;
7
+ //# sourceMappingURL=baseX.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"baseX.d.ts","sourceRoot":"","sources":["../src/baseX.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,SAAS,kCAAS,CAAA;AAE/B;;;GAGG;AACH,eAAO,MAAM,SAAS,kCAAiB,CAAA"}
package/dist/baseX.js ADDED
@@ -0,0 +1,11 @@
1
+ /*!
2
+ * Copyright (c) 2019-2026 Digital Bazaar, Inc.
3
+ */
4
+ import { base58, base64urlnopad } from '@scure/base';
5
+ export const base58btc = base58;
6
+ /**
7
+ * base64url must be RFC 4648 compliant for JWK interop, and JWK uses the
8
+ * unpadded form, hence `base64urlnopad`.
9
+ */
10
+ export const base64url = base64urlnopad;
11
+ //# sourceMappingURL=baseX.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"baseX.js","sourceRoot":"","sources":["../src/baseX.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAEpD,MAAM,CAAC,MAAM,SAAS,GAAG,MAAM,CAAA;AAE/B;;;GAGG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,cAAc,CAAA"}
@@ -0,0 +1,3 @@
1
+ declare const _default: Crypto;
2
+ export default _default;
3
+ //# sourceMappingURL=crypto.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":";AACA,wBAAgC"}
package/dist/crypto.js ADDED
@@ -0,0 +1,3 @@
1
+ // WebCrypto. Available natively in browsers and in Node (>=24).
2
+ export default globalThis.crypto;
3
+ //# sourceMappingURL=crypto.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto.js","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAChE,eAAe,UAAU,CAAC,MAAM,CAAA"}
@@ -0,0 +1,5 @@
1
+ /*!
2
+ * Copyright (c) 2018-2019 Digital Bazaar, Inc. All rights reserved.
3
+ */
4
+ export { Cipher } from './Cipher.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ /*!
2
+ * Copyright (c) 2018-2019 Digital Bazaar, Inc. All rights reserved.
3
+ */
4
+ export { Cipher } from './Cipher.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA"}
@@ -0,0 +1,119 @@
1
+ /*!
2
+ * Copyright (c) 2019-2026 Digital Bazaar, Inc.
3
+ */
4
+ /** Result of a content-encryption operation. */
5
+ export interface EncryptResult {
6
+ ciphertext: Uint8Array;
7
+ iv: Uint8Array;
8
+ tag: Uint8Array;
9
+ }
10
+ /** Options passed to a content-encryption algorithm's `encrypt`. */
11
+ export interface CipherEncryptOptions {
12
+ data: Uint8Array;
13
+ additionalData?: Uint8Array;
14
+ cek: Uint8Array;
15
+ }
16
+ /** Options passed to a content-encryption algorithm's `decrypt`. */
17
+ export interface CipherDecryptOptions {
18
+ ciphertext: Uint8Array;
19
+ iv: Uint8Array;
20
+ tag: Uint8Array;
21
+ additionalData?: Uint8Array;
22
+ cek: Uint8Array;
23
+ }
24
+ /** A content-encryption algorithm (e.g. A256GCM, XC20P, C20P). */
25
+ export interface CipherAlgorithm {
26
+ JWE_ENC: string;
27
+ generateKey(): Promise<Uint8Array>;
28
+ encrypt(options: CipherEncryptOptions): Promise<EncryptResult>;
29
+ decrypt(options: CipherDecryptOptions): Promise<Uint8Array | null>;
30
+ }
31
+ /** An ephemeral public key, encoded as a JWK. */
32
+ export interface Epk {
33
+ kty: string;
34
+ crv: string;
35
+ x?: string;
36
+ [key: string]: unknown;
37
+ }
38
+ /** An ephemeral key pair used during key agreement. */
39
+ export interface EphemeralKeyPair {
40
+ privateKey: Uint8Array;
41
+ publicKey: Uint8Array;
42
+ epk: Epk;
43
+ }
44
+ /** A key-encryption key (KEK) used to wrap/unwrap a CEK. */
45
+ export interface Kek {
46
+ algorithm: {
47
+ name: string;
48
+ };
49
+ wrapKey(options: {
50
+ unwrappedKey: Uint8Array;
51
+ }): Promise<string>;
52
+ unwrapKey(options: {
53
+ wrappedKey: string;
54
+ }): Promise<Uint8Array | null>;
55
+ }
56
+ /** Result of deriving a KEK from a static peer (encryption case). */
57
+ export interface KekFromStaticPeerResult {
58
+ kek: Kek;
59
+ epk: Epk;
60
+ apu: string;
61
+ apv: string;
62
+ ephemeralPublicKey: Uint8Array;
63
+ }
64
+ /** Result of deriving a KEK from an ephemeral peer (decryption case). */
65
+ export interface KekFromEphemeralPeerResult {
66
+ kek: Kek;
67
+ }
68
+ /**
69
+ * A key agreement key (KAK) API, as provided by the caller. External key
70
+ * implementations vary, so the non-identifying surface is left loose.
71
+ */
72
+ export interface KeyAgreementKey {
73
+ id: string;
74
+ algorithm?: any;
75
+ deriveSecret(options: {
76
+ publicKey: any;
77
+ }): Promise<Uint8Array>;
78
+ }
79
+ /** A key agreement algorithm (e.g. X25519, P-256). */
80
+ export interface KeyAgreementAlgorithm {
81
+ JWE_ALG: string;
82
+ generateEphemeralKeyPair(): Promise<EphemeralKeyPair>;
83
+ kekFromStaticPeer(options: {
84
+ ephemeralKeyPair: EphemeralKeyPair;
85
+ staticPublicKey: any;
86
+ }): Promise<KekFromStaticPeerResult>;
87
+ kekFromEphemeralPeer(options: {
88
+ keyAgreementKey: KeyAgreementKey;
89
+ epk: Epk;
90
+ }): Promise<KekFromEphemeralPeerResult>;
91
+ }
92
+ /** The JWE recipient header. */
93
+ export interface RecipientHeader {
94
+ kid?: string;
95
+ alg?: string;
96
+ epk?: Epk;
97
+ apu?: string;
98
+ apv?: string;
99
+ [key: string]: unknown;
100
+ }
101
+ /** A JWE recipient. */
102
+ export interface Recipient {
103
+ header: RecipientHeader;
104
+ encrypted_key?: string;
105
+ [key: string]: unknown;
106
+ }
107
+ /** A JSON Web Encryption (JWE) object. */
108
+ export interface JWE {
109
+ protected: string;
110
+ recipients: Recipient[];
111
+ iv: string;
112
+ ciphertext: string;
113
+ tag: string;
114
+ }
115
+ /** Resolves a key ID to a DH public key. */
116
+ export type KeyResolver = (options: {
117
+ id?: string;
118
+ }) => Promise<any>;
119
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,gDAAgD;AAChD,MAAM,WAAW,aAAa;IAC5B,UAAU,EAAE,UAAU,CAAA;IACtB,EAAE,EAAE,UAAU,CAAA;IACd,GAAG,EAAE,UAAU,CAAA;CAChB;AAED,oEAAoE;AACpE,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,UAAU,CAAA;IAChB,cAAc,CAAC,EAAE,UAAU,CAAA;IAC3B,GAAG,EAAE,UAAU,CAAA;CAChB;AAED,oEAAoE;AACpE,MAAM,WAAW,oBAAoB;IACnC,UAAU,EAAE,UAAU,CAAA;IACtB,EAAE,EAAE,UAAU,CAAA;IACd,GAAG,EAAE,UAAU,CAAA;IACf,cAAc,CAAC,EAAE,UAAU,CAAA;IAC3B,GAAG,EAAE,UAAU,CAAA;CAChB;AAED,kEAAkE;AAClE,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,MAAM,CAAA;IACf,WAAW,IAAI,OAAO,CAAC,UAAU,CAAC,CAAA;IAClC,OAAO,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,aAAa,CAAC,CAAA;IAC9D,OAAO,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAAA;C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,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG"}
package/dist/util.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare function stringToUint8Array(data: string | Uint8Array): Uint8Array;
2
+ //# sourceMappingURL=util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,UAAU,CASxE"}
package/dist/util.js ADDED
@@ -0,0 +1,11 @@
1
+ export function stringToUint8Array(data) {
2
+ if (typeof data === 'string') {
3
+ // convert data to Uint8Array
4
+ return new TextEncoder().encode(data);
5
+ }
6
+ if (!(data instanceof Uint8Array)) {
7
+ throw new TypeError('"data" be a string or Uint8Array.');
8
+ }
9
+ return data;
10
+ }
11
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.js","sourceRoot":"","sources":["../src/util.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,kBAAkB,CAAC,IAAyB;IAC1D,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,6BAA6B;QAC7B,OAAO,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IACvC,CAAC;IACD,IAAI,CAAC,CAAC,IAAI,YAAY,UAAU,CAAC,EAAE,CAAC;QAClC,MAAM,IAAI,SAAS,CAAC,mCAAmC,CAAC,CAAA;IAC1D,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC"}
package/package.json ADDED
@@ -0,0 +1,99 @@
1
+ {
2
+ "name": "@interop/minimal-cipher",
3
+ "version": "7.0.0",
4
+ "description": "Minimal encryption/decryption JWE library.",
5
+ "license": "BSD-3-Clause",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "react-native": "./dist/index.js",
11
+ "import": "./dist/index.js",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "module": "dist/index.js",
16
+ "browser": {
17
+ "./dist/algorithms/c20p.js": "./dist/algorithms/c20p-browser.js",
18
+ "./dist/algorithms/x25519-helper.js": "./dist/algorithms/x25519-helper-browser.js"
19
+ },
20
+ "types": "dist/index.d.ts",
21
+ "sideEffects": false,
22
+ "files": [
23
+ "dist",
24
+ "README.md",
25
+ "LICENSE"
26
+ ],
27
+ "scripts": {
28
+ "build": "pnpm run clear && tsc",
29
+ "clear": "rimraf dist/*",
30
+ "dev": "vite",
31
+ "fix": "eslint --fix src test && pnpm run format",
32
+ "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\" \"*.md\"",
33
+ "lint": "eslint src test",
34
+ "prepare": "pnpm run build",
35
+ "rebuild": "pnpm run clear && pnpm run build",
36
+ "test": "pnpm run fix && pnpm run lint && pnpm run test:node && pnpm run test:browser",
37
+ "test:browser": "playwright test",
38
+ "test:node": "vitest run",
39
+ "test:coverage": "vitest run --coverage"
40
+ },
41
+ "dependencies": {
42
+ "@interop/data-integrity-core": "^6.1.2",
43
+ "@interop/ecdsa-multikey": "^2.1.0",
44
+ "@noble/curves": "^2.2.0",
45
+ "@scure/base": "^2.2.0",
46
+ "@stablelib/chacha": "^2.0.1",
47
+ "@stablelib/chacha20poly1305": "^2.0.1"
48
+ },
49
+ "devDependencies": {
50
+ "@eslint/js": "^10.0.1",
51
+ "@interop/did-io": "^4.0.2",
52
+ "@interop/did-method-key": "^7.2.0",
53
+ "@interop/ed25519-verification-key": "^7.0.2",
54
+ "@interop/x25519-key-agreement-key": "^4.0.2",
55
+ "@playwright/test": "^1.60.0",
56
+ "@types/node": "^25.9.1",
57
+ "@vitest/coverage-v8": "^4.1.7",
58
+ "eslint": "^10.4.0",
59
+ "eslint-config-prettier": "^10.1.8",
60
+ "globals": "^17.6.0",
61
+ "prettier": "^3.8.3",
62
+ "rimraf": "^6.1.3",
63
+ "tweetnacl": "^1.0.3",
64
+ "typescript": "^5.9.3",
65
+ "typescript-eslint": "^8.59.4",
66
+ "vite": "^8.0.14",
67
+ "vitest": "^4.1.7"
68
+ },
69
+ "repository": {
70
+ "type": "git",
71
+ "url": "https://github.com/interop-alliance/minimal-cipher"
72
+ },
73
+ "keywords": [
74
+ "cipher",
75
+ "encryption",
76
+ "decryption",
77
+ "jwe",
78
+ "cwe",
79
+ "ChaCha20Poly1305",
80
+ "XChaCha20Poly1305",
81
+ "AES-GCM"
82
+ ],
83
+ "author": {
84
+ "name": "Digital Bazaar, Inc.",
85
+ "email": "support@digitalbazaar.com",
86
+ "url": "https://digitalbazaar.com/"
87
+ },
88
+ "bugs": {
89
+ "url": "https://github.com/interop-alliance/minimal-cipher/issues"
90
+ },
91
+ "homepage": "https://github.com/interop-alliance/minimal-cipher",
92
+ "publishConfig": {
93
+ "access": "public"
94
+ },
95
+ "packageManager": "pnpm@11.5.0",
96
+ "engines": {
97
+ "node": ">=24.0"
98
+ }
99
+ }