From 75568f893c9d9fe38cbc0202de3be85f87ddc28e Mon Sep 17 00:00:00 2001 From: larabr <7375870+larabr@users.noreply.github.com> Date: Fri, 13 Sep 2024 10:36:51 +0200 Subject: [PATCH] Switch to standardized version of ML-KEM --- package-lock.json | 53 ++++++++++--------- package.json | 2 +- rollup.config.js | 2 +- .../public_key/post_quantum/kem/ml_kem.js | 15 +++--- 4 files changed, 35 insertions(+), 37 deletions(-) diff --git a/package-lock.json b/package-lock.json index a80047d0c..01b771d53 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,7 @@ "@noble/curves": "^1.6.0", "@noble/ed25519": "^1.7.3", "@noble/hashes": "^1.5.0", - "@openpgp/crystals-kyber-js": "^1.1.1", + "@noble/post-quantum": "^0.2.0", "@openpgp/jsdoc": "^3.6.11", "@openpgp/seek-bzip": "^1.0.5-git", "@openpgp/tweetnacl": "^1.0.4-1", @@ -882,6 +882,32 @@ "url": "https://paulmillr.com/funding/" } }, + "node_modules/@noble/post-quantum": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/@noble/post-quantum/-/post-quantum-0.2.0.tgz", + "integrity": "sha512-6dXxLXv9qCdj22zTBIRN1J8RrF+OUWQD1vJHNcqCu4JAlSo7KnaRVc+ikDPqvgky43Rn7NGQoWqeo4wv8TAJ/g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@noble/hashes": "1.4.0" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/@noble/post-quantum/node_modules/@noble/hashes": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.4.0.tgz", + "integrity": "sha512-V1JJ1WTRUqHHrOSh597hURcMqVKVGL/ea3kv0gSnEdsEZ0/+VyPghM1lMNGc00z7CIQorSvbKpuJkxvuHbvdbg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 16" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", @@ -926,31 +952,6 @@ "node": ">=12.4.0" } }, - "node_modules/@openpgp/crystals-kyber-js": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/@openpgp/crystals-kyber-js/-/crystals-kyber-js-1.1.1.tgz", - "integrity": "sha512-Q4azKQpc2/SEPendXQs6IpnD2RNlPY2b7nwg5VNZ05FYYICnqYeH8R/NaTLJ2kVpitAWJpiNRC7bh7VlWz1T8g==", - "dev": true, - "dependencies": { - "@noble/hashes": "1.4.0" - }, - "engines": { - "node": ">=18.0.0" - } - }, - "node_modules/@openpgp/crystals-kyber-js/node_modules/@noble/hashes": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.4.0.tgz", - "integrity": "sha512-V1JJ1WTRUqHHrOSh597hURcMqVKVGL/ea3kv0gSnEdsEZ0/+VyPghM1lMNGc00z7CIQorSvbKpuJkxvuHbvdbg==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">= 16" - }, - "funding": { - "url": "https://paulmillr.com/funding/" - } - }, "node_modules/@openpgp/jsdoc": { "version": "3.6.11", "resolved": "https://registry.npmjs.org/@openpgp/jsdoc/-/jsdoc-3.6.11.tgz", diff --git a/package.json b/package.json index 54c44d81b..13a1cf527 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,7 @@ "@noble/curves": "^1.6.0", "@noble/ed25519": "^1.7.3", "@noble/hashes": "^1.5.0", - "@openpgp/crystals-kyber-js": "^1.1.1", + "@noble/post-quantum": "^0.2.0", "@openpgp/jsdoc": "^3.6.11", "@openpgp/seek-bzip": "^1.0.5-git", "@openpgp/tweetnacl": "^1.0.4-1", diff --git a/rollup.config.js b/rollup.config.js index 6c9febe2d..1f0e895a6 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -25,7 +25,7 @@ const wasmOptions = { const getChunkFileName = (chunkInfo, extension) => { // index files result in chunks named simply 'index', so we rename them to include the package name - if (chunkInfo.name === 'index' && chunkInfo.facadeModuleId) { + if (chunkInfo.name === 'index') { const packageName = chunkInfo.facadeModuleId.split('/').at(-2); // assume index file is under the root folder return `${packageName}.${extension}`; } diff --git a/src/crypto/public_key/post_quantum/kem/ml_kem.js b/src/crypto/public_key/post_quantum/kem/ml_kem.js index 12d62539f..72906e586 100644 --- a/src/crypto/public_key/post_quantum/kem/ml_kem.js +++ b/src/crypto/public_key/post_quantum/kem/ml_kem.js @@ -4,9 +4,8 @@ import util from '../../../../util'; export async function generate(algo) { switch (algo) { case enums.publicKey.pqc_mlkem_x25519: { - const { MlKem768 } = await import('@openpgp/crystals-kyber-js'); - const kyberInstance = new MlKem768(); - const [encapsulationKey, decapsulationKey] = await kyberInstance.generateKeyPair(); + const { ml_kem768 } = await import('@noble/post-quantum/ml-kem'); + const { publicKey: encapsulationKey, secretKey: decapsulationKey } = ml_kem768.keygen(); return { mlkemPublicKey: encapsulationKey, mlkemSecretKey: decapsulationKey }; } @@ -18,9 +17,8 @@ export async function generate(algo) { export async function encaps(algo, mlkemRecipientPublicKey) { switch (algo) { case enums.publicKey.pqc_mlkem_x25519: { - const { MlKem768 } = await import('@openpgp/crystals-kyber-js'); - const kyberInstance = new MlKem768(); - const [mlkemCipherText, mlkemKeyShare] = await kyberInstance.encap(mlkemRecipientPublicKey); + const { ml_kem768 } = await import('@noble/post-quantum/ml-kem'); + const { cipherText: mlkemCipherText, sharedSecret: mlkemKeyShare } = ml_kem768.encapsulate(mlkemRecipientPublicKey); return { mlkemCipherText, mlkemKeyShare }; } @@ -32,9 +30,8 @@ export async function encaps(algo, mlkemRecipientPublicKey) { export async function decaps(algo, mlkemCipherText, mlkemSecretKey) { switch (algo) { case enums.publicKey.pqc_mlkem_x25519: { - const { MlKem768 } = await import('@openpgp/crystals-kyber-js'); - const kyberInstance = new MlKem768(); - const mlkemKeyShare = await kyberInstance.decap(mlkemCipherText, mlkemSecretKey); + const { ml_kem768 } = await import('@noble/post-quantum/ml-kem'); + const mlkemKeyShare = ml_kem768.decapsulate(mlkemCipherText, mlkemSecretKey); return mlkemKeyShare; }