@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.
- package/LICENSE +27 -0
- package/README.md +220 -0
- package/dist/Cipher.d.ts +210 -0
- package/dist/Cipher.d.ts.map +1 -0
- package/dist/Cipher.js +313 -0
- package/dist/Cipher.js.map +1 -0
- package/dist/DecryptTransformer.d.ts +14 -0
- package/dist/DecryptTransformer.d.ts.map +1 -0
- package/dist/DecryptTransformer.js +125 -0
- package/dist/DecryptTransformer.js.map +1 -0
- package/dist/EncryptTransformer.d.ts +28 -0
- package/dist/EncryptTransformer.d.ts.map +1 -0
- package/dist/EncryptTransformer.js +94 -0
- package/dist/EncryptTransformer.js.map +1 -0
- package/dist/algorithms/a256gcm.d.ts +39 -0
- package/dist/algorithms/a256gcm.d.ts.map +1 -0
- package/dist/algorithms/a256gcm.js +101 -0
- package/dist/algorithms/a256gcm.js.map +1 -0
- package/dist/algorithms/aeskw.d.ts +5 -0
- package/dist/algorithms/aeskw.d.ts.map +1 -0
- package/dist/algorithms/aeskw.js +68 -0
- package/dist/algorithms/aeskw.js.map +1 -0
- package/dist/algorithms/c20p-browser.d.ts +51 -0
- package/dist/algorithms/c20p-browser.d.ts.map +1 -0
- package/dist/algorithms/c20p-browser.js +106 -0
- package/dist/algorithms/c20p-browser.js.map +1 -0
- package/dist/algorithms/c20p.d.ts +51 -0
- package/dist/algorithms/c20p.d.ts.map +1 -0
- package/dist/algorithms/c20p.js +110 -0
- package/dist/algorithms/c20p.js.map +1 -0
- package/dist/algorithms/ecdhkdf.d.ts +24 -0
- package/dist/algorithms/ecdhkdf.d.ts.map +1 -0
- package/dist/algorithms/ecdhkdf.js +76 -0
- package/dist/algorithms/ecdhkdf.js.map +1 -0
- package/dist/algorithms/fips.d.ts +8 -0
- package/dist/algorithms/fips.d.ts.map +1 -0
- package/dist/algorithms/fips.js +8 -0
- package/dist/algorithms/fips.js.map +1 -0
- package/dist/algorithms/p256.d.ts +24 -0
- package/dist/algorithms/p256.d.ts.map +1 -0
- package/dist/algorithms/p256.js +106 -0
- package/dist/algorithms/p256.js.map +1 -0
- package/dist/algorithms/recommended.d.ts +8 -0
- package/dist/algorithms/recommended.d.ts.map +1 -0
- package/dist/algorithms/recommended.js +8 -0
- package/dist/algorithms/recommended.js.map +1 -0
- package/dist/algorithms/x25519-helper-browser.d.ts +7 -0
- package/dist/algorithms/x25519-helper-browser.d.ts.map +1 -0
- package/dist/algorithms/x25519-helper-browser.js +25 -0
- package/dist/algorithms/x25519-helper-browser.js.map +1 -0
- package/dist/algorithms/x25519-helper.d.ts +7 -0
- package/dist/algorithms/x25519-helper.d.ts.map +1 -0
- package/dist/algorithms/x25519-helper.js +51 -0
- package/dist/algorithms/x25519-helper.js.map +1 -0
- package/dist/algorithms/x25519.d.ts +44 -0
- package/dist/algorithms/x25519.d.ts.map +1 -0
- package/dist/algorithms/x25519.js +112 -0
- package/dist/algorithms/x25519.js.map +1 -0
- package/dist/algorithms/xc20p.d.ts +40 -0
- package/dist/algorithms/xc20p.d.ts.map +1 -0
- package/dist/algorithms/xc20p.js +160 -0
- package/dist/algorithms/xc20p.js.map +1 -0
- package/dist/baseX.d.ts +7 -0
- package/dist/baseX.d.ts.map +1 -0
- package/dist/baseX.js +11 -0
- package/dist/baseX.js.map +1 -0
- package/dist/crypto.d.ts +3 -0
- package/dist/crypto.d.ts.map +1 -0
- package/dist/crypto.js +3 -0
- package/dist/crypto.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +119 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/util.d.ts +2 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +11 -0
- package/dist/util.js.map +1 -0
- package/package.json +99 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fips.d.ts","sourceRoot":"","sources":["../../src/algorithms/fips.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,MAAM,MAAM,cAAc,CAAA;AACtC,OAAO,KAAK,YAAY,MAAM,WAAW,CAAA;AAEzC,OAAO,EAAE,MAAM,EAAE,CAAA;AACjB,OAAO,EAAE,YAAY,EAAE,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fips.js","sourceRoot":"","sources":["../../src/algorithms/fips.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,MAAM,MAAM,cAAc,CAAA;AACtC,OAAO,KAAK,YAAY,MAAM,WAAW,CAAA;AAEzC,OAAO,EAAE,MAAM,EAAE,CAAA;AACjB,OAAO,EAAE,YAAY,EAAE,CAAA"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { IMultikeyDocument } from '@interop/data-integrity-core';
|
|
2
|
+
import type { Epk, EphemeralKeyPair, KekFromEphemeralPeerResult, KekFromStaticPeerResult, KeyAgreementKey } from '../types.js';
|
|
3
|
+
export declare const JWE_ALG = "ECDH-ES+A256KW";
|
|
4
|
+
export declare function kekFromEphemeralPeer({ keyAgreementKey, epk }: {
|
|
5
|
+
keyAgreementKey: KeyAgreementKey;
|
|
6
|
+
epk: Epk;
|
|
7
|
+
}): Promise<KekFromEphemeralPeerResult>;
|
|
8
|
+
/**
|
|
9
|
+
* (Encryption case) Generates KEK from ephemeral DH private key and a peer's
|
|
10
|
+
* public static key.
|
|
11
|
+
*
|
|
12
|
+
* @param {object} options - Options hashmap.
|
|
13
|
+
* @param {object} options.ephemeralKeyPair - Ephemeral key pair.
|
|
14
|
+
* @param {object} options.staticPublicKey - Static public key.
|
|
15
|
+
*
|
|
16
|
+
* @returns {Promise<object>} - Resolves with kek object derived from static
|
|
17
|
+
* peer.
|
|
18
|
+
*/
|
|
19
|
+
export declare function kekFromStaticPeer({ ephemeralKeyPair, staticPublicKey }: {
|
|
20
|
+
ephemeralKeyPair: EphemeralKeyPair;
|
|
21
|
+
staticPublicKey: IMultikeyDocument;
|
|
22
|
+
}): Promise<KekFromStaticPeerResult>;
|
|
23
|
+
export declare function generateEphemeralKeyPair(): Promise<EphemeralKeyPair>;
|
|
24
|
+
//# sourceMappingURL=p256.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"p256.d.ts","sourceRoot":"","sources":["../../src/algorithms/p256.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAA;AAGrE,OAAO,KAAK,EACV,GAAG,EACH,gBAAgB,EAChB,0BAA0B,EAC1B,uBAAuB,EACvB,eAAe,EAChB,MAAM,aAAa,CAAA;AAEpB,eAAO,MAAM,OAAO,mBAAmB,CAAA;AAIvC,wBAAsB,oBAAoB,CAAC,EACzC,eAAe,EACf,GAAG,EACJ,EAAE;IACD,eAAe,EAAE,eAAe,CAAA;IAChC,GAAG,EAAE,GAAG,CAAA;CACT,GAAG,OAAO,CAAC,0BAA0B,CAAC,CAqCtC;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,iBAAiB,CAAC,EACtC,gBAAgB,EAChB,eAAe,EAChB,EAAE;IACD,gBAAgB,EAAE,gBAAgB,CAAA;IAElC,eAAe,EAAE,iBAAiB,CAAA;CACnC,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAyBnC;AAED,wBAAsB,wBAAwB,IAAI,OAAO,CAAC,gBAAgB,CAAC,CAgB1E"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) 2019-2023 Digital Bazaar, Inc. All rights reserved.
|
|
3
|
+
*/
|
|
4
|
+
import { base64url } from '../baseX.js';
|
|
5
|
+
import * as EcdsaMultikey from '@interop/ecdsa-multikey';
|
|
6
|
+
import { createKek } from './aeskw.js';
|
|
7
|
+
import { deriveKey } from './ecdhkdf.js';
|
|
8
|
+
export const JWE_ALG = 'ECDH-ES+A256KW';
|
|
9
|
+
// Decryption case: get Kek from a private key agreement key and a
|
|
10
|
+
// peer's public ephemeral DH key encoded as an `epk`
|
|
11
|
+
export async function kekFromEphemeralPeer({ keyAgreementKey, epk }) {
|
|
12
|
+
if (!(epk && typeof epk === 'object')) {
|
|
13
|
+
throw new TypeError('"epk" must be an object.');
|
|
14
|
+
}
|
|
15
|
+
if (epk.kty !== 'EC') {
|
|
16
|
+
throw new Error('"epk.kty" must be the string "EC".');
|
|
17
|
+
}
|
|
18
|
+
if (epk.crv !== 'P-256') {
|
|
19
|
+
throw new Error('"epk.crv" must be the string "P-256".');
|
|
20
|
+
}
|
|
21
|
+
const jwk = { ...epk };
|
|
22
|
+
jwk.key_ops = ['deriveBits'];
|
|
23
|
+
// the `epk` is validated above as an EC P-256 JWK, but the loose JWE-header
|
|
24
|
+
// `Epk` shape does not structurally satisfy the strict EC `Jwk`
|
|
25
|
+
const publicKey = await EcdsaMultikey.fromJwk({
|
|
26
|
+
jwk: jwk,
|
|
27
|
+
secretKey: true
|
|
28
|
+
});
|
|
29
|
+
// export to multikey key for Web KMS transport and to raw for `producerInfo`
|
|
30
|
+
const [ephemeralPublicKey, { publicKey: rawPublicKey }] = await Promise.all([
|
|
31
|
+
publicKey.export({ publicKey: true, includeContext: true }),
|
|
32
|
+
publicKey.export({ raw: true })
|
|
33
|
+
]);
|
|
34
|
+
// is it safe to use IDs like in rfc7518 or does
|
|
35
|
+
// https://tools.ietf.org/html/rfc7748#section-7 pose any issues?
|
|
36
|
+
const encoder = new TextEncoder();
|
|
37
|
+
// "Party U Info"
|
|
38
|
+
const producerInfo = rawPublicKey;
|
|
39
|
+
// "Party V Info"
|
|
40
|
+
const consumerInfo = encoder.encode(keyAgreementKey.id);
|
|
41
|
+
const secret = await keyAgreementKey.deriveSecret({
|
|
42
|
+
publicKey: ephemeralPublicKey
|
|
43
|
+
});
|
|
44
|
+
const keyData = await deriveKey({ secret, producerInfo, consumerInfo });
|
|
45
|
+
return { kek: await createKek({ keyData }) };
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* (Encryption case) Generates KEK from ephemeral DH private key and a peer's
|
|
49
|
+
* public static key.
|
|
50
|
+
*
|
|
51
|
+
* @param {object} options - Options hashmap.
|
|
52
|
+
* @param {object} options.ephemeralKeyPair - Ephemeral key pair.
|
|
53
|
+
* @param {object} options.staticPublicKey - Static public key.
|
|
54
|
+
*
|
|
55
|
+
* @returns {Promise<object>} - Resolves with kek object derived from static
|
|
56
|
+
* peer.
|
|
57
|
+
*/
|
|
58
|
+
export async function kekFromStaticPeer({ ephemeralKeyPair, staticPublicKey }) {
|
|
59
|
+
if (!staticPublicKey) {
|
|
60
|
+
throw new Error('"staticPublicKey" is required.');
|
|
61
|
+
}
|
|
62
|
+
// static key must be a P-256 multikey
|
|
63
|
+
const keyAgreement = true;
|
|
64
|
+
const remotePublicKey = await EcdsaMultikey.from(staticPublicKey, keyAgreement);
|
|
65
|
+
const encoder = new TextEncoder();
|
|
66
|
+
// "Party U Info"
|
|
67
|
+
const producerInfo = ephemeralKeyPair.publicKey;
|
|
68
|
+
// "Party V Info"
|
|
69
|
+
const consumerInfo = encoder.encode(staticPublicKey.id);
|
|
70
|
+
const secret = await _deriveSecret({ ephemeralKeyPair, remotePublicKey });
|
|
71
|
+
const keyData = await deriveKey({ secret, producerInfo, consumerInfo });
|
|
72
|
+
return {
|
|
73
|
+
kek: await createKek({ keyData }),
|
|
74
|
+
epk: ephemeralKeyPair.epk,
|
|
75
|
+
apu: base64url.encode(producerInfo),
|
|
76
|
+
apv: base64url.encode(consumerInfo),
|
|
77
|
+
ephemeralPublicKey: ephemeralKeyPair.publicKey
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
export async function generateEphemeralKeyPair() {
|
|
81
|
+
// generate P-256 ephemeral public key
|
|
82
|
+
const keyPair = await EcdsaMultikey.generate({
|
|
83
|
+
curve: 'P-256',
|
|
84
|
+
keyAgreement: true
|
|
85
|
+
});
|
|
86
|
+
const { secretKey: privateKey, publicKey } = await keyPair.export({
|
|
87
|
+
secretKey: true,
|
|
88
|
+
raw: true
|
|
89
|
+
});
|
|
90
|
+
const epk = await EcdsaMultikey.toJwk({ keyPair, secretKey: true });
|
|
91
|
+
return {
|
|
92
|
+
privateKey: privateKey,
|
|
93
|
+
publicKey: publicKey,
|
|
94
|
+
epk: epk
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
async function _deriveSecret({ ephemeralKeyPair, remotePublicKey }) {
|
|
98
|
+
const jwk = { ...ephemeralKeyPair.epk };
|
|
99
|
+
jwk.key_ops = ['deriveBits'];
|
|
100
|
+
const privateKey = await EcdsaMultikey.fromJwk({
|
|
101
|
+
jwk: jwk,
|
|
102
|
+
secretKey: true
|
|
103
|
+
});
|
|
104
|
+
return privateKey.deriveSecret({ remotePublicKey });
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=p256.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"p256.js","sourceRoot":"","sources":["../../src/algorithms/p256.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,KAAK,aAAa,MAAM,yBAAyB,CAAA;AAGxD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AASxC,MAAM,CAAC,MAAM,OAAO,GAAG,gBAAgB,CAAA;AAEvC,kEAAkE;AAClE,qDAAqD;AACrD,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,EACzC,eAAe,EACf,GAAG,EAIJ;IACC,IAAI,CAAC,CAAC,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ,CAAC,EAAE,CAAC;QACtC,MAAM,IAAI,SAAS,CAAC,0BAA0B,CAAC,CAAA;IACjD,CAAC;IACD,IAAI,GAAG,CAAC,GAAG,KAAK,IAAI,EAAE,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AAU,EAAE,SAAS,EAAE,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC;QAChE,SAAS,EAAE,IAAI;QACf,GAAG,EAAE,IAAI;KACV,CAAC,CAAA;IACF,MAAM,GAAG,GAAG,MAAM,aAAa,CAAC,KAAK,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAA;IACnE,OAAO;QACL,UAAU,EAAE,UAAwB;QACpC,SAAS,EAAE,SAAuB;QAClC,GAAG,EAAE,GAAqB;KAC3B,CAAA;AACH,CAAC;AAED,KAAK,UAAU,aAAa,CAAC,EAC3B,gBAAgB,EAChB,eAAe,EAKhB;IACC,MAAM,GAAG,GAAG,EAAE,GAAG,gBAAgB,CAAC,GAAG,EAAE,CAAA;IACvC,GAAG,CAAC,OAAO,GAAG,CAAC,YAAY,CAAC,CAAA;IAC5B,MAAM,UAAU,GAAG,MAAM,aAAa,CAAC,OAAO,CAAC;QAC7C,GAAG,EAAE,GAAqB;QAC1B,SAAS,EAAE,IAAI;KAChB,CAAC,CAAA;IACF,OAAO,UAAU,CAAC,YAAY,CAAC,EAAE,eAAe,EAAE,CAAC,CAAA;AACrD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recommended.d.ts","sourceRoot":"","sources":["../../src/algorithms/recommended.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,MAAM,MAAM,YAAY,CAAA;AACpC,OAAO,KAAK,YAAY,MAAM,aAAa,CAAA;AAE3C,OAAO,EAAE,MAAM,EAAE,CAAA;AACjB,OAAO,EAAE,YAAY,EAAE,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recommended.js","sourceRoot":"","sources":["../../src/algorithms/recommended.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,MAAM,MAAM,YAAY,CAAA;AACpC,OAAO,KAAK,YAAY,MAAM,aAAa,CAAA;AAE3C,OAAO,EAAE,MAAM,EAAE,CAAA;AACjB,OAAO,EAAE,YAAY,EAAE,CAAA"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { EphemeralKeyPair } from '../types.js';
|
|
2
|
+
export declare function generateEphemeralKeyPair(): Promise<EphemeralKeyPair>;
|
|
3
|
+
export declare function deriveSecret({ privateKey, remotePublicKey }: {
|
|
4
|
+
privateKey: Uint8Array;
|
|
5
|
+
remotePublicKey: Uint8Array;
|
|
6
|
+
}): Promise<Uint8Array>;
|
|
7
|
+
//# sourceMappingURL=x25519-helper-browser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"x25519-helper-browser.d.ts","sourceRoot":"","sources":["../../src/algorithms/x25519-helper-browser.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAEnD,wBAAsB,wBAAwB,IAAI,OAAO,CAAC,gBAAgB,CAAC,CAa1E;AAED,wBAAsB,YAAY,CAAC,EACjC,UAAU,EACV,eAAe,EAChB,EAAE;IACD,UAAU,EAAE,UAAU,CAAA;IACtB,eAAe,EAAE,UAAU,CAAA;CAC5B,GAAG,OAAO,CAAC,UAAU,CAAC,CAGtB"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) 2019-2026 Digital Bazaar, Inc.
|
|
3
|
+
*/
|
|
4
|
+
import { base64url } from '../baseX.js';
|
|
5
|
+
import crypto from '../crypto.js';
|
|
6
|
+
import { x25519 } from '@noble/curves/ed25519.js';
|
|
7
|
+
export async function generateEphemeralKeyPair() {
|
|
8
|
+
// generate X25519 ephemeral public key
|
|
9
|
+
const privateKey = await crypto.getRandomValues(new Uint8Array(32));
|
|
10
|
+
const publicKey = x25519.scalarMultBase(privateKey);
|
|
11
|
+
return {
|
|
12
|
+
privateKey,
|
|
13
|
+
publicKey,
|
|
14
|
+
epk: {
|
|
15
|
+
kty: 'OKP',
|
|
16
|
+
crv: 'X25519',
|
|
17
|
+
x: base64url.encode(publicKey)
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export async function deriveSecret({ privateKey, remotePublicKey }) {
|
|
22
|
+
// `scalarMult` takes secret key as param 1, public key as param 2
|
|
23
|
+
return x25519.scalarMult(privateKey, remotePublicKey);
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=x25519-helper-browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"x25519-helper-browser.js","sourceRoot":"","sources":["../../src/algorithms/x25519-helper-browser.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,MAAM,MAAM,cAAc,CAAA;AACjC,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAA;AAGjD,MAAM,CAAC,KAAK,UAAU,wBAAwB;IAC5C,uCAAuC;IACvC,MAAM,UAAU,GAAG,MAAM,MAAM,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC,CAAA;IACnE,MAAM,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC,UAAU,CAAC,CAAA;IACnD,OAAO;QACL,UAAU;QACV,SAAS;QACT,GAAG,EAAE;YACH,GAAG,EAAE,KAAK;YACV,GAAG,EAAE,QAAQ;YACb,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,SAAS,CAAC;SAC/B;KACF,CAAA;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,EACjC,UAAU,EACV,eAAe,EAIhB;IACC,kEAAkE;IAClE,OAAO,MAAM,CAAC,UAAU,CAAC,UAAU,EAAE,eAAe,CAAC,CAAA;AACvD,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { EphemeralKeyPair } from '../types.js';
|
|
2
|
+
export declare function generateEphemeralKeyPair(): Promise<EphemeralKeyPair>;
|
|
3
|
+
export declare function deriveSecret({ privateKey, remotePublicKey }: {
|
|
4
|
+
privateKey: Uint8Array;
|
|
5
|
+
remotePublicKey: Uint8Array;
|
|
6
|
+
}): Promise<Uint8Array>;
|
|
7
|
+
//# sourceMappingURL=x25519-helper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"x25519-helper.d.ts","sourceRoot":"","sources":["../../src/algorithms/x25519-helper.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAanD,wBAAsB,wBAAwB,IAAI,OAAO,CAAC,gBAAgB,CAAC,CAoB1E;AAED,wBAAsB,YAAY,CAAC,EACjC,UAAU,EACV,eAAe,EAChB,EAAE;IACD,UAAU,EAAE,UAAU,CAAA;IACtB,eAAe,EAAE,UAAU,CAAA;CAC5B,GAAG,OAAO,CAAC,UAAU,CAAC,CAetB"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) 2019-2022 Digital Bazaar, Inc. All rights reserved.
|
|
3
|
+
*/
|
|
4
|
+
import { base64url } from '../baseX.js';
|
|
5
|
+
import * as crypto from 'node:crypto';
|
|
6
|
+
import * as util from 'node:util';
|
|
7
|
+
const { promisify } = util;
|
|
8
|
+
const generateKeyPairAsync = promisify(crypto.generateKeyPair);
|
|
9
|
+
const PUBLIC_KEY_DER_PREFIX = new Uint8Array([
|
|
10
|
+
48, 42, 48, 5, 6, 3, 43, 101, 110, 3, 33, 0
|
|
11
|
+
]);
|
|
12
|
+
const PRIVATE_KEY_DER_PREFIX = new Uint8Array([
|
|
13
|
+
48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 110, 4, 34, 4, 32
|
|
14
|
+
]);
|
|
15
|
+
export async function generateEphemeralKeyPair() {
|
|
16
|
+
// generate X25519 ephemeral public key
|
|
17
|
+
const publicKeyEncoding = { format: 'der', type: 'spki' };
|
|
18
|
+
const privateKeyEncoding = { format: 'der', type: 'pkcs8' };
|
|
19
|
+
const { publicKey: publicDerBytes, privateKey: privateDerBytes } = await generateKeyPairAsync('x25519', {
|
|
20
|
+
publicKeyEncoding,
|
|
21
|
+
privateKeyEncoding
|
|
22
|
+
});
|
|
23
|
+
const publicKey = publicDerBytes.slice(12, 12 + 32);
|
|
24
|
+
const privateKey = privateDerBytes.slice(16, 16 + 32);
|
|
25
|
+
return {
|
|
26
|
+
privateKey,
|
|
27
|
+
publicKey,
|
|
28
|
+
epk: {
|
|
29
|
+
kty: 'OKP',
|
|
30
|
+
crv: 'X25519',
|
|
31
|
+
x: base64url.encode(publicKey)
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export async function deriveSecret({ privateKey, remotePublicKey }) {
|
|
36
|
+
const nodePrivateKey = crypto.createPrivateKey({
|
|
37
|
+
key: Buffer.concat([PRIVATE_KEY_DER_PREFIX, privateKey]),
|
|
38
|
+
format: 'der',
|
|
39
|
+
type: 'pkcs8'
|
|
40
|
+
});
|
|
41
|
+
const nodePublicKey = crypto.createPublicKey({
|
|
42
|
+
key: Buffer.concat([PUBLIC_KEY_DER_PREFIX, remotePublicKey]),
|
|
43
|
+
format: 'der',
|
|
44
|
+
type: 'spki'
|
|
45
|
+
});
|
|
46
|
+
return crypto.diffieHellman({
|
|
47
|
+
privateKey: nodePrivateKey,
|
|
48
|
+
publicKey: nodePublicKey
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=x25519-helper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"x25519-helper.js","sourceRoot":"","sources":["../../src/algorithms/x25519-helper.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AAEjC,MAAM,EAAE,SAAS,EAAE,GAAG,IAAI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MAAM,CAAC,SAAS,CAAC;SAC/B;KACF,CAAA;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,EACjC,UAAU,EACV,eAAe,EAIhB;IACC,MAAM,cAAc,GAAG,MAAM,CAAC,gBAAgB,CAAC;QAC7C,GAAG,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,sBAAsB,EAAE,UAAU,CAAC,CAAC;QACxD,MAAM,EAAE,KAAK;QACb,IAAI,EAAE,OAAO;KACd,CAAC,CAAA;IACF,MAAM,aAAa,GAAG,MAAM,CAAC,eAAe,CAAC;QAC3C,GAAG,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,qBAAqB,EAAE,eAAe,CAAC,CAAC;QAC5D,MAAM,EAAE,KAAK;QACb,IAAI,EAAE,MAAM;KACb,CAAC,CAAA;IACF,OAAO,MAAM,CAAC,aAAa,CAAC;QAC1B,UAAU,EAAE,cAAc;QAC1B,SAAS,EAAE,aAAa;KACzB,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { deriveSecret, generateEphemeralKeyPair } from './x25519-helper.js';
|
|
2
|
+
import type { Epk, EphemeralKeyPair, KekFromEphemeralPeerResult, KekFromStaticPeerResult, KeyAgreementKey } from '../types.js';
|
|
3
|
+
export declare const MULTICODEC_X25519_PUB_HEADER: Uint8Array<ArrayBuffer>;
|
|
4
|
+
export declare const JWE_ALG = "ECDH-ES+A256KW";
|
|
5
|
+
export { generateEphemeralKeyPair, deriveSecret };
|
|
6
|
+
export declare function kekFromEphemeralPeer({ keyAgreementKey, epk }: {
|
|
7
|
+
keyAgreementKey: KeyAgreementKey;
|
|
8
|
+
epk: Epk;
|
|
9
|
+
}): Promise<KekFromEphemeralPeerResult>;
|
|
10
|
+
/**
|
|
11
|
+
* (Encryption case) Generates KEK from ephemeral DH private key and a peer's
|
|
12
|
+
* public static key.
|
|
13
|
+
*
|
|
14
|
+
* @param {object} options - Options hashmap.
|
|
15
|
+
* @param {object} options.ephemeralKeyPair - Ephemeral key pair.
|
|
16
|
+
* @param {object} options.staticPublicKey - Static public key.
|
|
17
|
+
*
|
|
18
|
+
* @returns {Promise<object>} - Resolves with kek object derived from static
|
|
19
|
+
* peer.
|
|
20
|
+
*/
|
|
21
|
+
export declare function kekFromStaticPeer({ ephemeralKeyPair, staticPublicKey }: {
|
|
22
|
+
ephemeralKeyPair: EphemeralKeyPair;
|
|
23
|
+
staticPublicKey: any;
|
|
24
|
+
}): Promise<KekFromStaticPeerResult>;
|
|
25
|
+
/**
|
|
26
|
+
* Adds a multicodec prefix to a given bytes array representing a public
|
|
27
|
+
* or private key, and multibase-encodes the result.
|
|
28
|
+
*
|
|
29
|
+
* @param {Uint8Array} header - Multicodec x25519 pub or pri key header, varint.
|
|
30
|
+
* @param {Uint8Array} bytes - Byte array representing a public or private key.
|
|
31
|
+
*
|
|
32
|
+
* @returns {string} Base58-btc encoded key (with multicodec prefix).
|
|
33
|
+
*/
|
|
34
|
+
export declare function multibaseEncode(header: Uint8Array, bytes: Uint8Array): string;
|
|
35
|
+
/**
|
|
36
|
+
* Decodes a given string as a multibase-encoded multicodec value.
|
|
37
|
+
*
|
|
38
|
+
* @param {Uint8Array} header - Expected header bytes for the multicodec value.
|
|
39
|
+
* @param {string} text - Multibase encoded string to decode.
|
|
40
|
+
*
|
|
41
|
+
* @returns {Uint8Array} Decoded bytes.
|
|
42
|
+
*/
|
|
43
|
+
export declare function multibaseDecode(header: Uint8Array, text: string): Uint8Array;
|
|
44
|
+
//# sourceMappingURL=x25519.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"x25519.d.ts","sourceRoot":"","sources":["../../src/algorithms/x25519.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,YAAY,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAA;AAG3E,OAAO,KAAK,EACV,GAAG,EACH,gBAAgB,EAChB,0BAA0B,EAC1B,uBAAuB,EACvB,eAAe,EAChB,MAAM,aAAa,CAAA;AAMpB,eAAO,MAAM,4BAA4B,yBAA+B,CAAA;AACxE,eAAO,MAAM,OAAO,mBAAmB,CAAA;AACvC,OAAO,EAAE,wBAAwB,EAAE,YAAY,EAAE,CAAA;AAIjD,wBAAsB,oBAAoB,CAAC,EACzC,eAAe,EACf,GAAG,EACJ,EAAE;IACD,eAAe,EAAE,eAAe,CAAA;IAChC,GAAG,EAAE,GAAG,CAAA;CACT,GAAG,OAAO,CAAC,0BAA0B,CAAC,CAiCtC;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,iBAAiB,CAAC,EACtC,gBAAgB,EAChB,eAAe,EAChB,EAAE;IACD,gBAAgB,EAAE,gBAAgB,CAAA;IAElC,eAAe,EAAE,GAAG,CAAA;CACrB,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAyBnC;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,UAAU,GAAG,MAAM,CAK7E;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,UAAU,CAQ5E"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) 2019-2023 Digital Bazaar, Inc. All rights reserved.
|
|
3
|
+
*/
|
|
4
|
+
import { base58btc, base64url } from '../baseX.js';
|
|
5
|
+
import { deriveSecret, generateEphemeralKeyPair } from './x25519-helper.js';
|
|
6
|
+
import { createKek } from './aeskw.js';
|
|
7
|
+
import { deriveKey } from './ecdhkdf.js';
|
|
8
|
+
const KEY_TYPE = 'X25519KeyAgreementKey2020';
|
|
9
|
+
// multibase base58-btc header
|
|
10
|
+
const MULTIBASE_BASE58BTC_HEADER = 'z';
|
|
11
|
+
// multicodec X25519-pub header as varint
|
|
12
|
+
export const MULTICODEC_X25519_PUB_HEADER = new Uint8Array([0xec, 0x01]);
|
|
13
|
+
export const JWE_ALG = 'ECDH-ES+A256KW';
|
|
14
|
+
export { generateEphemeralKeyPair, deriveSecret };
|
|
15
|
+
// Decryption case: get Kek from a private key agreement key and a
|
|
16
|
+
// peer's public ephemeral DH key encoded as an `epk`
|
|
17
|
+
export async function kekFromEphemeralPeer({ keyAgreementKey, epk }) {
|
|
18
|
+
if (!(epk && typeof epk === 'object')) {
|
|
19
|
+
throw new TypeError('"epk" must be an object.');
|
|
20
|
+
}
|
|
21
|
+
if (epk.kty !== 'OKP') {
|
|
22
|
+
throw new Error('"epk.kty" must be the string "OKP".');
|
|
23
|
+
}
|
|
24
|
+
if (epk.crv !== 'X25519') {
|
|
25
|
+
throw new Error('"epk.crv" must be the string "X25519".');
|
|
26
|
+
}
|
|
27
|
+
// decode public key material
|
|
28
|
+
const publicKey = base64url.decode(epk.x);
|
|
29
|
+
// convert to LD key for Web KMS
|
|
30
|
+
const ephemeralPublicKey = {
|
|
31
|
+
type: KEY_TYPE,
|
|
32
|
+
publicKeyMultibase: multibaseEncode(MULTICODEC_X25519_PUB_HEADER, publicKey)
|
|
33
|
+
};
|
|
34
|
+
// safe to use IDs like in rfc7518 or does
|
|
35
|
+
// https://tools.ietf.org/html/rfc7748#section-7 pose any issues?
|
|
36
|
+
const encoder = new TextEncoder();
|
|
37
|
+
// "Party U Info"
|
|
38
|
+
const producerInfo = publicKey;
|
|
39
|
+
// "Party V Info"
|
|
40
|
+
const consumerInfo = encoder.encode(keyAgreementKey.id);
|
|
41
|
+
const secret = await keyAgreementKey.deriveSecret({
|
|
42
|
+
publicKey: ephemeralPublicKey
|
|
43
|
+
});
|
|
44
|
+
const keyData = await deriveKey({ secret, producerInfo, consumerInfo });
|
|
45
|
+
return {
|
|
46
|
+
kek: await createKek({ keyData })
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* (Encryption case) Generates KEK from ephemeral DH private key and a peer's
|
|
51
|
+
* public static key.
|
|
52
|
+
*
|
|
53
|
+
* @param {object} options - Options hashmap.
|
|
54
|
+
* @param {object} options.ephemeralKeyPair - Ephemeral key pair.
|
|
55
|
+
* @param {object} options.staticPublicKey - Static public key.
|
|
56
|
+
*
|
|
57
|
+
* @returns {Promise<object>} - Resolves with kek object derived from static
|
|
58
|
+
* peer.
|
|
59
|
+
*/
|
|
60
|
+
export async function kekFromStaticPeer({ ephemeralKeyPair, staticPublicKey }) {
|
|
61
|
+
if (!staticPublicKey) {
|
|
62
|
+
throw new Error('"staticPublicKey" is required.');
|
|
63
|
+
}
|
|
64
|
+
// static key must be an X25519 multikey
|
|
65
|
+
const remotePublicKey = multibaseDecode(MULTICODEC_X25519_PUB_HEADER, staticPublicKey.publicKeyMultibase);
|
|
66
|
+
const encoder = new TextEncoder();
|
|
67
|
+
// "Party U Info"
|
|
68
|
+
const producerInfo = ephemeralKeyPair.publicKey;
|
|
69
|
+
// "Party V Info"
|
|
70
|
+
const consumerInfo = encoder.encode(staticPublicKey.id);
|
|
71
|
+
const { privateKey } = ephemeralKeyPair;
|
|
72
|
+
const secret = await deriveSecret({ privateKey, remotePublicKey });
|
|
73
|
+
const keyData = await deriveKey({ secret, producerInfo, consumerInfo });
|
|
74
|
+
return {
|
|
75
|
+
kek: await createKek({ keyData }),
|
|
76
|
+
epk: ephemeralKeyPair.epk,
|
|
77
|
+
apu: base64url.encode(producerInfo),
|
|
78
|
+
apv: base64url.encode(consumerInfo),
|
|
79
|
+
ephemeralPublicKey: ephemeralKeyPair.publicKey
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Adds a multicodec prefix to a given bytes array representing a public
|
|
84
|
+
* or private key, and multibase-encodes the result.
|
|
85
|
+
*
|
|
86
|
+
* @param {Uint8Array} header - Multicodec x25519 pub or pri key header, varint.
|
|
87
|
+
* @param {Uint8Array} bytes - Byte array representing a public or private key.
|
|
88
|
+
*
|
|
89
|
+
* @returns {string} Base58-btc encoded key (with multicodec prefix).
|
|
90
|
+
*/
|
|
91
|
+
export function multibaseEncode(header, bytes) {
|
|
92
|
+
const mcBytes = new Uint8Array(header.length + bytes.length);
|
|
93
|
+
mcBytes.set(header);
|
|
94
|
+
mcBytes.set(bytes, header.length);
|
|
95
|
+
return MULTIBASE_BASE58BTC_HEADER + base58btc.encode(mcBytes);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Decodes a given string as a multibase-encoded multicodec value.
|
|
99
|
+
*
|
|
100
|
+
* @param {Uint8Array} header - Expected header bytes for the multicodec value.
|
|
101
|
+
* @param {string} text - Multibase encoded string to decode.
|
|
102
|
+
*
|
|
103
|
+
* @returns {Uint8Array} Decoded bytes.
|
|
104
|
+
*/
|
|
105
|
+
export function multibaseDecode(header, text) {
|
|
106
|
+
const mcValue = base58btc.decode(text.slice(1));
|
|
107
|
+
if (!header.every((val, i) => mcValue[i] === val)) {
|
|
108
|
+
throw new Error('Multibase value does not have expected header.');
|
|
109
|
+
}
|
|
110
|
+
return mcValue.slice(header.length);
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=x25519.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"x25519.js","sourceRoot":"","sources":["../../src/algorithms/x25519.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAClD,OAAO,EAAE,YAAY,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAA;AAC3E,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AASxC,MAAM,QAAQ,GAAG,2BAA2B,CAAA;AAC5C,8BAA8B;AAC9B,MAAM,0BAA0B,GAAG,GAAG,CAAA;AACtC,yCAAyC;AACzC,MAAM,CAAC,MAAM,4BAA4B,GAAG,IAAI,UAAU,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAA;AACxE,MAAM,CAAC,MAAM,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AAC,CAAA;IAC5D,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;IACnB,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,CAAA;IACjC,OAAO,0BAA0B,GAAG,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;AAC/D,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,eAAe,CAAC,MAAkB,EAAE,IAAY;IAC9D,MAAM,OAAO,GAAG,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;IAE/C,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,EAAE,CAAC;QAClD,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAA;IACnE,CAAC;IAED,OAAO,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;AACrC,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { CipherDecryptOptions, CipherEncryptOptions, EncryptResult } from '../types.js';
|
|
2
|
+
export declare const JWE_ENC = "XC20P";
|
|
3
|
+
/**
|
|
4
|
+
* Generates a content encryption key (CEK). The 256-bit key is intended to be
|
|
5
|
+
* used as a XChaCha20Poly1305 (draft-irtf-cfrg-xchacha-01) key.
|
|
6
|
+
*
|
|
7
|
+
* @returns {Promise<Uint8Array>} - Resolves to the generated key.
|
|
8
|
+
*/
|
|
9
|
+
export declare function generateKey(): Promise<Uint8Array>;
|
|
10
|
+
/**
|
|
11
|
+
* Encrypts some data. The data will be encrypted using the given
|
|
12
|
+
* 256-bit XChaCha20Poly1305 (draft-irtf-cfrg-xchacha-01) content encryption
|
|
13
|
+
* key (CEK).
|
|
14
|
+
*
|
|
15
|
+
* @param {object} options - The options to use.
|
|
16
|
+
* @param {Uint8Array} options.data - The data to encrypt.
|
|
17
|
+
* @param {Uint8Array} [options.additionalData] - Optional additional
|
|
18
|
+
* authentication data.
|
|
19
|
+
* @param {Uint8Array} options.cek - The content encryption key to use.
|
|
20
|
+
*
|
|
21
|
+
* @returns {Promise<object>} - Resolves to `{ciphertext, iv, tag}`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function encrypt({ data, additionalData, cek }: CipherEncryptOptions): Promise<EncryptResult>;
|
|
24
|
+
/**
|
|
25
|
+
* Decrypts some encrypted data. The data must have been encrypted using
|
|
26
|
+
* the given XChaCha20Poly1305 (draft-irtf-cfrg-xchacha-01) content encryption
|
|
27
|
+
* key (CEK).
|
|
28
|
+
*
|
|
29
|
+
* @param {object} options - The options to use.
|
|
30
|
+
* @param {Uint8Array} options.ciphertext - The data to decrypt.
|
|
31
|
+
* @param {Uint8Array} options.iv - The initialization vector (aka nonce).
|
|
32
|
+
* @param {Uint8Array} options.tag - The authentication tag.
|
|
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<Uint8Array>} The decrypted data.
|
|
38
|
+
*/
|
|
39
|
+
export declare function decrypt({ ciphertext, iv, tag, additionalData, cek }: CipherDecryptOptions): Promise<Uint8Array>;
|
|
40
|
+
//# sourceMappingURL=xc20p.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"xc20p.d.ts","sourceRoot":"","sources":["../../src/algorithms/xc20p.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,oBAAoB,EACpB,oBAAoB,EACpB,aAAa,EACd,MAAM,aAAa,CAAA;AAYpB,eAAO,MAAM,OAAO,UAAU,CAAA;AAE9B;;;;;GAKG;AACH,wBAAsB,WAAW,IAAI,OAAO,CAAC,UAAU,CAAC,CAGvD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,OAAO,CAAC,EAC5B,IAAI,EACJ,cAAc,EACd,GAAG,EACJ,EAAE,oBAAoB,GAAG,OAAO,CAAC,aAAa,CAAC,CAqB/C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,OAAO,CAAC,EAC5B,UAAU,EACV,EAAE,EACF,GAAG,EACH,cAAc,EACd,GAAG,EACJ,EAAE,oBAAoB,GAAG,OAAO,CAAC,UAAU,CAAC,CA8B5C"}
|