From 58e5abef550f8f7b26c8d90f795a3462a79ca09a Mon Sep 17 00:00:00 2001 From: Ajitomi Daisuke Date: Thu, 7 Nov 2024 23:30:02 +0900 Subject: [PATCH] hybridke-x-wing: remove _importRawKey. --- packages/hybridkem-x-wing/src/xWing.ts | 72 +++----- .../test/hybridkemXWing.test.ts | 159 ++++++++++++++++-- 2 files changed, 170 insertions(+), 61 deletions(-) diff --git a/packages/hybridkem-x-wing/src/xWing.ts b/packages/hybridkem-x-wing/src/xWing.ts index 84c5ad504..9453db724 100644 --- a/packages/hybridkem-x-wing/src/xWing.ts +++ b/packages/hybridkem-x-wing/src/xWing.ts @@ -218,13 +218,33 @@ export class XWing implements KemInterface { ): Promise { await this._setup(); try { - if (format === "raw") { - return await this._importRawKey(key as ArrayBuffer, isPublic); + let ret: Uint8Array; + if (format === "jwk") { + if (key instanceof ArrayBuffer || key instanceof Uint8Array) { + throw new Error("Invalid jwk key format"); + } + ret = await this._importJWK(key as JsonWebKey, isPublic); + } else { + if (key instanceof ArrayBuffer) { + ret = new Uint8Array(key); + } else if (key instanceof Uint8Array) { + ret = key; + } else { + throw new Error("Invalid key format"); + } + } + if (isPublic && ret.byteLength !== this.publicKeySize) { + throw new Error("Invalid length of the key"); } - if (key instanceof ArrayBuffer) { - throw new Error("Invalid jwk key format"); + if (!isPublic && ret.byteLength !== this.privateKeySize) { + throw new Error("Invalid length of the key"); } - return await this._importJWK(key as JsonWebKey, isPublic); + return new XCryptoKey( + ALG_NAME, + ret, + isPublic ? "public" : "private", + isPublic ? [] : KEM_USAGES, + ); } catch (e: unknown) { throw new DeserializeError(e); } @@ -406,32 +426,10 @@ export class XWing implements KemInterface { }); } - private _importRawKey( - key: ArrayBuffer, - isPublic: boolean, - ): Promise { - return new Promise((resolve, reject) => { - if (isPublic && key.byteLength !== this.publicKeySize) { - reject(new Error("Invalid length of the key")); - } - if (!isPublic && key.byteLength !== this.privateKeySize) { - reject(new Error("Invalid length of the key")); - } - resolve( - new XCryptoKey( - ALG_NAME, - new Uint8Array(key), - isPublic ? "public" : "private", - isPublic ? [] : KEM_USAGES, - ), - ); - }); - } - private _importJWK( key: JsonWebKeyExtended, isPublic: boolean, - ): Promise { + ): Promise { return new Promise((resolve, reject) => { if (typeof key.kty === "undefined" || key.kty !== "AKP") { reject(new Error(`Invalid kty: ${key.kty}`)); @@ -449,15 +447,7 @@ export class XWing implements KemInterface { ) { reject(new Error("Invalid key: `key_ops` should be ['deriveBits']")); } - resolve( - new XCryptoKey( - ALG_NAME, - base64UrlToBytes(key.priv as string), - "private", - KEM_USAGES, - ), - ); - return; + resolve(base64UrlToBytes(key.priv as string)); } if (typeof key.priv !== "undefined") { reject(new Error("Invalid key: `priv` should not be set")); @@ -468,13 +458,7 @@ export class XWing implements KemInterface { if (typeof key.key_ops !== "undefined" && key.key_ops.length > 0) { reject(new Error("Invalid key: `key_ops` should not be set")); } - resolve( - new XCryptoKey( - ALG_NAME, - base64UrlToBytes(key.pub as string), - "public", - ), - ); + resolve(base64UrlToBytes(key.pub as string)); }); } } diff --git a/packages/hybridkem-x-wing/test/hybridkemXWing.test.ts b/packages/hybridkem-x-wing/test/hybridkemXWing.test.ts index d77982e5a..b64d96c79 100644 --- a/packages/hybridkem-x-wing/test/hybridkemXWing.test.ts +++ b/packages/hybridkem-x-wing/test/hybridkemXWing.test.ts @@ -76,7 +76,28 @@ describe("XWing", () => { }); describe("importKey", () => { - it("with valid raw", async () => { + it("with valid raw - as ArrayBuffer", async () => { + const kem = new XWing(); + const pkR = await kem.importKey( + "raw", + hexToBytes( + "e2236b35a8c24b39b10aa1323a96a919a2ced88400633a7b07131713fc14b2b5b19cfc3da5fa1a92c49f25513e0fd30d6b1611c9ab9635d7086727a4b7d21d34244e66969cf15b3b2a785329f61b096b277ea037383479a6b556de7231fe4b7fa9c9ac24c0699a0018a5253401bacfa905ca816573e56a2d2e067e9b7287533ba13a937dedb31fa44baced40769923610034ae31e619a170245199b3c5c39864859fe1b4c9717a07c30495bdfb98a0a002ccf56c1286cef5041dede3c44cf16bf562c7448518026b3d8b9940680abd38a1575fd27b58da063bfac32c39c30869374c05c1aeb1898b6b303cc68be455346ee0af699636224a148ca2aea10463111c709f69b69c70ce8538746698c4c60a9aef0030c7924ceec42a5d36816f545eae13293460b3acb37ea0e13d70e4aa78686da398a8397c08eaf96882113fe4f7bad4da40b0501e1c753efe73053c87014e8661c33099afe8bede414a5b1aa27d8392b3e131e9a70c1055878240cad0f40d5fe3cdf85236ead97e2a97448363b2808caafd516cd25052c5c362543c2517e4acd0e60ec07163009b6425fc32277acee71c24bab53ed9f29e74c66a0a3564955998d76b96a9a8b50d1635a4d7a67eb42df5644d330457293a8042f53cc7a69288f17ed55827e82b28e82665a86a14fbd96645eca8172c044f83bc0d8c0b4c8626985631ca87af829068f1358963cb333664ca482763ba3b3bb208577f9ba6ac62c25f76592743b64be519317714cb4102cb7b2f9a25b2b4f0615de31decd9ca55026d6da0b65111b16fe52feed8a487e144462a6dba93728f500b6ffc49e515569ef25fed17aff520507368253525860f58be3be61c964604a6ac814e6935596402a520a4670b3d284318866593d15a4bb01c35e3e587ee0c67d2880d6f2407fb7a70712b838deb96c5d7bf2b44bcf6038ccbe33fbcf51a54a584fe90083c91c7a6d43d4fb15f48c60c2fd66e0a8aad4ad64e5c42bb8877c0ebec2b5e387c8a988fdc23beb9e16c8757781e0a1499c61e138c21f216c29d076979871caa6942bafc090544bee99b54b16cb9a9a364d6246d9f42cce53c66b59c45c8f9ae9299a75d15180c3c952151a91b7a10772429dc4cbae6fcc622fa8018c63439f890630b9928db6bb7f9438ae4065ed34d73d486f3f52f90f0807dc88dfdd8c728e954f1ac35c06c000ce41a0582580e3bb57b672972890ac5e7988e7850657116f1b57d0809aaedec0bede1ae148148311c6f7e317346e5189fb8cd635b986f8c0bdd27641c584b778b3a911a80be1c9692ab8e1bbb12839573cce19df183b45835bbb55052f9fc66a1678ef2a36dea78411e6c8d60501b4e60592d13698a943b509185db912e2ea10be06171236b327c71716094c964a68b03377f513a05bcd99c1f346583bb052977a10a12adfc758034e5617da4c1276585e5774e1f3b9978b09d0e9c44d3bc86151c43aad185712717340223ac381d21150a04294e97bb13bbda21b5a182b6da969e19a7fd072737fa8e880a53c2428e3d049b7d2197405296ddb361912a7bcf4827ced611d0c7a7da104dde4322095339f64a61d5bb108ff0bf4d780cae509fb22c256914193ff7349042581237d522828824ee3bdfd07fb03f1f942d2ea179fe722f06cc03de5b69859edb06eff389b27dce59844570216223593d4ba32d9abac8cd049040ef6534", + ).buffer, + true, + ); + const skR = await kem.importKey( + "raw", + hexToBytes( + "7f9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26", + ).buffer, + false, + ); + const res = await kem.encap({ recipientPublicKey: pkR }); + const ssR = await kem.decap({ enc: res.enc, recipientKey: skR }); + assertEquals(res.sharedSecret, ssR); + }); + + it("with valid raw - as Uint8Array", async () => { const kem = new XWing(); const pkR = await kem.importKey( "raw", @@ -180,6 +201,72 @@ describe("XWing", () => { assertEquals(res.sharedSecret, ssR); }); + it("with invalid raw - public key with valid jwk", async () => { + const kem = new XWing(); + const jwkPkR = { + kty: "AKP", + kid: "01", + alg: "X-Wing", + pub: + "4iNrNajCSzmxCqEyOpapGaLO2IQAYzp7BxMXE_wUsrWxnPw9pfoaksSfJVE-D9MNaxYRyauWNdcIZyekt9IdNCROZpac8Vs7KnhTKfYbCWsnfqA3ODR5prVW3nIx_kt_qcmsJMBpmgAYpSU0AbrPqQXKgWVz5WotLgZ-m3KHUzuhOpN97bMfpEus7UB2mSNhADSuMeYZoXAkUZmzxcOYZIWf4bTJcXoHwwSVvfuYoKACzPVsEobO9QQd7ePETPFr9WLHRIUYAms9i5lAaAq9OKFXX9J7WNoGO_rDLDnDCGk3TAXBrrGJi2swPMaL5FU0buCvaZY2IkoUjKKuoQRjERxwn2m2nHDOhTh0ZpjExgqa7wAwx5JM7sQqXTaBb1RerhMpNGCzrLN-oOE9cOSqeGhto5ioOXwI6vloghE_5Pe61NpAsFAeHHU-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sQj_C_TXgMrlCfsiwlaRQZP_c0kEJYEjfVIoKIJO4739B_sD8flC0uoXn-ci8GzAPeW2mFntsG7_OJsn3OWYRFcCFiI1k9S6MtmrrIzQSQQO9lNA", + key_ops: [], + }; + await assertRejects( + () => + kem.importKey( + "raw", + jwkPkR, + true, + ), + DeserializeError, + ); + }); + + it("with invalid raw - public key with invalid length of the key", async () => { + const kem = new XWing(); + await assertRejects( + () => + kem.importKey( + "raw", + hexToBytes( + "236b35a8c24b39b10aa1323a96a919a2ced88400633a7b07131713fc14b2b5b19cfc3da5fa1a92c49f25513e0fd30d6b1611c9ab9635d7086727a4b7d21d34244e66969cf15b3b2a785329f61b096b277ea037383479a6b556de7231fe4b7fa9c9ac24c0699a0018a5253401bacfa905ca816573e56a2d2e067e9b7287533ba13a937dedb31fa44baced40769923610034ae31e619a170245199b3c5c39864859fe1b4c9717a07c30495bdfb98a0a002ccf56c1286cef5041dede3c44cf16bf562c7448518026b3d8b9940680abd38a1575fd27b58da063bfac32c39c30869374c05c1aeb1898b6b303cc68be455346ee0af699636224a148ca2aea10463111c709f69b69c70ce8538746698c4c60a9aef0030c7924ceec42a5d36816f545eae13293460b3acb37ea0e13d70e4aa78686da398a8397c08eaf96882113fe4f7bad4da40b0501e1c753efe73053c87014e8661c33099afe8bede414a5b1aa27d8392b3e131e9a70c1055878240cad0f40d5fe3cdf85236ead97e2a97448363b2808caafd516cd25052c5c362543c2517e4acd0e60ec07163009b6425fc32277acee71c24bab53ed9f29e74c66a0a3564955998d76b96a9a8b50d1635a4d7a67eb42df5644d330457293a8042f53cc7a69288f17ed55827e82b28e82665a86a14fbd96645eca8172c044f83bc0d8c0b4c8626985631ca87af829068f1358963cb333664ca482763ba3b3bb208577f9ba6ac62c25f76592743b64be519317714cb4102cb7b2f9a25b2b4f0615de31decd9ca55026d6da0b65111b16fe52feed8a487e144462a6dba93728f500b6ffc49e515569ef25fed17aff520507368253525860f58be3be61c964604a6ac814e6935596402a520a4670b3d284318866593d15a4bb01c35e3e587ee0c67d2880d6f2407fb7a70712b838deb96c5d7bf2b44bcf6038ccbe33fbcf51a54a584fe90083c91c7a6d43d4fb15f48c60c2fd66e0a8aad4ad64e5c42bb8877c0ebec2b5e387c8a988fdc23beb9e16c8757781e0a1499c61e138c21f216c29d076979871caa6942bafc090544bee99b54b16cb9a9a364d6246d9f42cce53c66b59c45c8f9ae9299a75d15180c3c952151a91b7a10772429dc4cbae6fcc622fa8018c63439f890630b9928db6bb7f9438ae4065ed34d73d486f3f52f90f0807dc88dfdd8c728e954f1ac35c06c000ce41a0582580e3bb57b672972890ac5e7988e7850657116f1b57d0809aaedec0bede1ae148148311c6f7e317346e5189fb8cd635b986f8c0bdd27641c584b778b3a911a80be1c9692ab8e1bbb12839573cce19df183b45835bbb55052f9fc66a1678ef2a36dea78411e6c8d60501b4e60592d13698a943b509185db912e2ea10be06171236b327c71716094c964a68b03377f513a05bcd99c1f346583bb052977a10a12adfc758034e5617da4c1276585e5774e1f3b9978b09d0e9c44d3bc86151c43aad185712717340223ac381d21150a04294e97bb13bbda21b5a182b6da969e19a7fd072737fa8e880a53c2428e3d049b7d2197405296ddb361912a7bcf4827ced611d0c7a7da104dde4322095339f64a61d5bb108ff0bf4d780cae509fb22c256914193ff7349042581237d522828824ee3bdfd07fb03f1f942d2ea179fe722f06cc03de5b69859edb06eff389b27dce59844570216223593d4ba32d9abac8cd049040ef6534", + ), + true, + ), + DeserializeError, + ); + }); + + it("with invalid raw - private key with invalid length of the key", async () => { + const kem = new XWing(); + await assertRejects( + () => + kem.importKey( + "raw", + hexToBytes( + "9c2ba4e88f827d616045507605853ed73b8093f6efbc88eb1a6eacfa66ef26", + ), + false, + ), + DeserializeError, + ); + }); + + it("with invalid jwk - public key with valid raw key", async () => { + const kem = new XWing(); + await assertRejects( + () => + kem.importKey( + "jwk", + hexToBytes( + "e2236b35a8c24b39b10aa1323a96a919a2ced88400633a7b07131713fc14b2b5b19cfc3da5fa1a92c49f25513e0fd30d6b1611c9ab9635d7086727a4b7d21d34244e66969cf15b3b2a785329f61b096b277ea037383479a6b556de7231fe4b7fa9c9ac24c0699a0018a5253401bacfa905ca816573e56a2d2e067e9b7287533ba13a937dedb31fa44baced40769923610034ae31e619a170245199b3c5c39864859fe1b4c9717a07c30495bdfb98a0a002ccf56c1286cef5041dede3c44cf16bf562c7448518026b3d8b9940680abd38a1575fd27b58da063bfac32c39c30869374c05c1aeb1898b6b303cc68be455346ee0af699636224a148ca2aea10463111c709f69b69c70ce8538746698c4c60a9aef0030c7924ceec42a5d36816f545eae13293460b3acb37ea0e13d70e4aa78686da398a8397c08eaf96882113fe4f7bad4da40b0501e1c753efe73053c87014e8661c33099afe8bede414a5b1aa27d8392b3e131e9a70c1055878240cad0f40d5fe3cdf85236ead97e2a97448363b2808caafd516cd25052c5c362543c2517e4acd0e60ec07163009b6425fc32277acee71c24bab53ed9f29e74c66a0a3564955998d76b96a9a8b50d1635a4d7a67eb42df5644d330457293a8042f53cc7a69288f17ed55827e82b28e82665a86a14fbd96645eca8172c044f83bc0d8c0b4c8626985631ca87af829068f1358963cb333664ca482763ba3b3bb208577f9ba6ac62c25f76592743b64be519317714cb4102cb7b2f9a25b2b4f0615de31decd9ca55026d6da0b65111b16fe52feed8a487e144462a6dba93728f500b6ffc49e515569ef25fed17aff520507368253525860f58be3be61c964604a6ac814e6935596402a520a4670b3d284318866593d15a4bb01c35e3e587ee0c67d2880d6f2407fb7a70712b838deb96c5d7bf2b44bcf6038ccbe33fbcf51a54a584fe90083c91c7a6d43d4fb15f48c60c2fd66e0a8aad4ad64e5c42bb8877c0ebec2b5e387c8a988fdc23beb9e16c8757781e0a1499c61e138c21f216c29d076979871caa6942bafc090544bee99b54b16cb9a9a364d6246d9f42cce53c66b59c45c8f9ae9299a75d15180c3c952151a91b7a10772429dc4cbae6fcc622fa8018c63439f890630b9928db6bb7f9438ae4065ed34d73d486f3f52f90f0807dc88dfdd8c728e954f1ac35c06c000ce41a0582580e3bb57b672972890ac5e7988e7850657116f1b57d0809aaedec0bede1ae148148311c6f7e317346e5189fb8cd635b986f8c0bdd27641c584b778b3a911a80be1c9692ab8e1bbb12839573cce19df183b45835bbb55052f9fc66a1678ef2a36dea78411e6c8d60501b4e60592d13698a943b509185db912e2ea10be06171236b327c71716094c964a68b03377f513a05bcd99c1f346583bb052977a10a12adfc758034e5617da4c1276585e5774e1f3b9978b09d0e9c44d3bc86151c43aad185712717340223ac381d21150a04294e97bb13bbda21b5a182b6da969e19a7fd072737fa8e880a53c2428e3d049b7d2197405296ddb361912a7bcf4827ced611d0c7a7da104dde4322095339f64a61d5bb108ff0bf4d780cae509fb22c256914193ff7349042581237d522828824ee3bdfd07fb03f1f942d2ea179fe722f06cc03de5b69859edb06eff389b27dce59844570216223593d4ba32d9abac8cd049040ef6534", + ), + true, + ), + DeserializeError, + ); + }); + it("with invalid jwk - public key with invalid kty", async () => { const kem = new XWing(); @@ -252,6 +339,24 @@ describe("XWing", () => { ); }); + it("with invalid jwk - public key with invalid length of the key", async () => { + const kem = new XWing(); + + const jwk = { + kty: "AKP", + kid: "01", + alg: "X-Wing", + pub: + "NrNajCSzmxCqEyOpapGaLO2IQAYzp7BxMXE_wUsrWxnPw9pfoaksSfJVE-D9MNaxYRyauWNdcIZyekt9IdNCROZpac8Vs7KnhTKfYbCWsnfqA3ODR5prVW3nIx_kt_qcmsJMBpmgAYpSU0AbrPqQXKgWVz5WotLgZ-m3KHUzuhOpN97bMfpEus7UB2mSNhADSuMeYZoXAkUZmzxcOYZIWf4bTJcXoHwwSVvfuYoKACzPVsEobO9QQd7ePETPFr9WLHRIUYAms9i5lAaAq9OKFXX9J7WNoGO_rDLDnDCGk3TAXBrrGJi2swPMaL5FU0buCvaZY2IkoUjKKuoQRjERxwn2m2nHDOhTh0ZpjExgqa7wAwx5JM7sQqXTaBb1RerhMpNGCzrLN-oOE9cOSqeGhto5ioOXwI6vloghE_5Pe61NpAsFAeHHU-_nMFPIcBToZhwzCZr-i-3kFKWxqifYOSs-Ex6ac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IJO4739B_sD8flC0uoXn-ci8GzAPeW2mFntsG7_OJsn3OWYRFcCFiI1k9S6MtmrrIzQSQQO9lNA", + key_ops: [], + }; + // assert + await assertRejects( + () => kem.importKey("jwk", jwk, false), + DeserializeError, + ); + }); + it("with invalid jwk - public key with priv", async () => { const kem = new XWing(); @@ -392,6 +497,23 @@ describe("XWing", () => { ); }); + it("with invalid jwk - private key with invalid length of the key", async () => { + const kem = new XWing(); + + const jwk = { + kty: "AKP", + kid: "01", + alg: "X-Wing", + priv: "wrpOiPgn1hYEVQdgWFPtc7gJP277yI6xpurPpm7yY", + key_ops: ["deriveBits"], + }; + // assert + await assertRejects( + () => kem.importKey("jwk", jwk, false), + DeserializeError, + ); + }); + it("with invalid jwk - private key without priv", async () => { const kem = new XWing(); @@ -497,11 +619,6 @@ describe("README examples", () => { }); it("should work normally with JWK", async () => { - const suite = new CipherSuite({ - kem: new XWing(), - kdf: new HkdfSha256(), - aead: new Aes256Gcm(), - }); // const pub = bytesToBase64Url( // hexToBytes( // "e2236b35a8c24b39b10aa1323a96a919a2ced88400633a7b07131713fc14b2b5b19cfc3da5fa1a92c49f25513e0fd30d6b1611c9ab9635d7086727a4b7d21d34244e66969cf15b3b2a785329f61b096b277ea037383479a6b556de7231fe4b7fa9c9ac24c0699a0018a5253401bacfa905ca816573e56a2d2e067e9b7287533ba13a937dedb31fa44baced40769923610034ae31e619a170245199b3c5c39864859fe1b4c9717a07c30495bdfb98a0a002ccf56c1286cef5041dede3c44cf16bf562c7448518026b3d8b9940680abd38a1575fd27b58da063bfac32c39c30869374c05c1aeb1898b6b303cc68be455346ee0af699636224a148ca2aea10463111c709f69b69c70ce8538746698c4c60a9aef0030c7924ceec42a5d36816f545eae13293460b3acb37ea0e13d70e4aa78686da398a8397c08eaf96882113fe4f7bad4da40b0501e1c753efe73053c87014e8661c33099afe8bede414a5b1aa27d8392b3e131e9a70c1055878240cad0f40d5fe3cdf85236ead97e2a97448363b2808caafd516cd25052c5c362543c2517e4acd0e60ec07163009b6425fc32277acee71c24bab53ed9f29e74c66a0a3564955998d76b96a9a8b50d1635a4d7a67eb42df5644d330457293a8042f53cc7a69288f17ed55827e82b28e82665a86a14fbd96645eca8172c044f83bc0d8c0b4c8626985631ca87af829068f1358963cb333664ca482763ba3b3bb208577f9ba6ac62c25f76592743b64be519317714cb4102cb7b2f9a25b2b4f0615de31decd9ca55026d6da0b65111b16fe52feed8a487e144462a6dba93728f500b6ffc49e515569ef25fed17aff520507368253525860f58be3be61c964604a6ac814e6935596402a520a4670b3d284318866593d15a4bb01c35e3e587ee0c67d2880d6f2407fb7a70712b838deb96c5d7bf2b44bcf6038ccbe33fbcf51a54a584fe90083c91c7a6d43d4fb15f48c60c2fd66e0a8aad4ad64e5c42bb8877c0ebec2b5e387c8a988fdc23beb9e16c8757781e0a1499c61e138c21f216c29d076979871caa6942bafc090544bee99b54b16cb9a9a364d6246d9f42cce53c66b59c45c8f9ae9299a75d15180c3c952151a91b7a10772429dc4cbae6fcc622fa8018c63439f890630b9928db6bb7f9438ae4065ed34d73d486f3f52f90f0807dc88dfdd8c728e954f1ac35c06c000ce41a0582580e3bb57b672972890ac5e7988e7850657116f1b57d0809aaedec0bede1ae148148311c6f7e317346e5189fb8cd635b986f8c0bdd27641c584b778b3a911a80be1c9692ab8e1bbb12839573cce19df183b45835bbb55052f9fc66a1678ef2a36dea78411e6c8d60501b4e60592d13698a943b509185db912e2ea10be06171236b327c71716094c964a68b03377f513a05bcd99c1f346583bb052977a10a12adfc758034e5617da4c1276585e5774e1f3b9978b09d0e9c44d3bc86151c43aad185712717340223ac381d21150a04294e97bb13bbda21b5a182b6da969e19a7fd072737fa8e880a53c2428e3d049b7d2197405296ddb361912a7bcf4827ced611d0c7a7da104dde4322095339f64a61d5bb108ff0bf4d780cae509fb22c256914193ff7349042581237d522828824ee3bdfd07fb03f1f942d2ea179fe722f06cc03de5b69859edb06eff389b27dce59844570216223593d4ba32d9abac8cd049040ef6534", @@ -514,7 +631,16 @@ describe("README examples", () => { // ), // ); // console.log(priv); - const jwkPkR = { + + const suite = new CipherSuite({ + kem: new XWing(), + kdf: new HkdfSha256(), + aead: new Aes256Gcm(), + }); + + // NOTE: The following support for JWKs with the AKP key type is experimental. + // Please be aware that the specifications are subject to change without notice. + const jwkPub = { kty: "AKP", kid: "01", alg: "X-Wing", @@ -522,29 +648,28 @@ describe("README examples", () => { "4iNrNajCSzmxCqEyOpapGaLO2IQAYzp7BxMXE_wUsrWxnPw9pfoaksSfJVE-D9MNaxYRyauWNdcIZyekt9IdNCROZpac8Vs7KnhTKfYbCWsnfqA3ODR5prVW3nIx_kt_qcmsJMBpmgAYpSU0AbrPqQXKgWVz5WotLgZ-m3KHUzuhOpN97bMfpEus7UB2mSNhADSuMeYZoXAkUZmzxcOYZIWf4bTJcXoHwwSVvfuYoKACzPVsEobO9QQd7ePETPFr9WLHRIUYAms9i5lAaAq9OKFXX9J7WNoGO_rDLDnDCGk3TAXBrrGJi2swPMaL5FU0buCvaZY2IkoUjKKuoQRjERxwn2m2nHDOhTh0ZpjExgqa7wAwx5JM7sQqXTaBb1RerhMpNGCzrLN-oOE9cOSqeGhto5ioOXwI6vloghE_5Pe61NpAsFAeHHU-_nMFPIcBToZhwzCZr-i-3kFKWxqifYOSs-Ex6acMEFWHgkDK0PQNX-PN-FI26tl-KpdEg2OygIyq_VFs0lBSxcNiVDwlF-Ss0OYOwHFjAJtkJfwyJ3rO5xwkurU-2fKedMZqCjVklVmY12uWqai1DRY1pNemfrQt9WRNMwRXKTqAQvU8x6aSiPF-1Vgn6Cso6CZlqGoU-9lmReyoFywET4O8DYwLTIYmmFYxyoevgpBo8TWJY8szNmTKSCdjujs7sghXf5umrGLCX3ZZJ0O2S-UZMXcUy0ECy3svmiWytPBhXeMd7NnKVQJtbaC2URGxb-Uv7tikh-FERiptupNyj1ALb_xJ5RVWnvJf7Rev9SBQc2glNSWGD1i-O-YclkYEpqyBTmk1WWQCpSCkZws9KEMYhmWT0VpLsBw14-WH7gxn0ogNbyQH-3pwcSuDjeuWxde_K0S89gOMy-M_vPUaVKWE_pAIPJHHptQ9T7FfSMYML9ZuCoqtStZOXEK7iHfA6-wrXjh8ipiP3CO-ueFsh1d4HgoUmcYeE4wh8hbCnQdpeYccqmlCuvwJBUS-6ZtUsWy5qaNk1iRtn0LM5TxmtZxFyPmukpmnXRUYDDyVIVGpG3oQdyQp3Ey65vzGIvqAGMY0OfiQYwuZKNtrt_lDiuQGXtNNc9SG8_UvkPCAfciN_djHKOlU8aw1wGwADOQaBYJYDju1e2cpcokKxeeYjnhQZXEW8bV9CAmq7ewL7eGuFIFIMRxvfjFzRuUYn7jNY1uYb4wL3SdkHFhLd4s6kRqAvhyWkquOG7sSg5VzzOGd8YO0WDW7tVBS-fxmoWeO8qNt6nhBHmyNYFAbTmBZLRNpipQ7UJGF25EuLqEL4GFxI2syfHFxYJTJZKaLAzd_UToFvNmcHzRlg7sFKXehChKt_HWANOVhfaTBJ2WF5XdOHzuZeLCdDpxE07yGFRxDqtGFcScXNAIjrDgdIRUKBClOl7sTu9ohtaGCttqWnhmn_QcnN_qOiApTwkKOPQSbfSGXQFKW3bNhkSp7z0gnztYR0Men2hBN3kMiCVM59kph1bsQj_C_TXgMrlCfsiwlaRQZP_c0kEJYEjfVIoKIJO4739B_sD8flC0uoXn-ci8GzAPeW2mFntsG7_OJsn3OWYRFcCFiI1k9S6MtmrrIzQSQQO9lNA", key_ops: [], }; - const pkR = await suite.kem.importKey("jwk", jwkPkR, true); + const pk = await suite.kem.importKey("jwk", jwkPub, true); const sender = await suite.createSenderContext({ - recipientPublicKey: pkR, + recipientPublicKey: pk, }); - assertEquals(sender.enc.byteLength, suite.kem.encSize); - const jwkSkR = { + const jwkPriv = { kty: "AKP", kid: "01", alg: "X-Wing", priv: "f5wrpOiPgn1hYEVQdgWFPtc7gJP277yI6xpurPpm7yY", key_ops: ["deriveBits"], }; - const skR = await suite.kem.importKey("jwk", jwkSkR, false); + const sk = await suite.kem.importKey("jwk", jwkPriv, false); const recipient = await suite.createRecipientContext({ - recipientKey: skR, + recipientKey: sk, enc: sender.enc, }); - const ct = await sender.seal( - new TextEncoder().encode("my-secret-message"), + const encrypted = await sender.seal( + new TextEncoder().encode("Hellow world!"), ); - const pt = await recipient.open(ct); - assertEquals(new TextDecoder().decode(pt), "my-secret-message"); + const pt = await recipient.open(encrypted); + assertEquals(new TextDecoder().decode(pt), "Hellow world!"); }); }); });