From 6b9af054401294274dfb8f6addf5061a4451aa05 Mon Sep 17 00:00:00 2001 From: Marin Petrunic Date: Thu, 27 Apr 2023 10:21:08 +0200 Subject: [PATCH 1/3] replace stablelib hashes with noble Signed-off-by: Marin Petrunic --- package.json | 7 +++---- src/crypto/stablelib.ts | 27 ++++++++++++++------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 75aa3350..9b8327a1 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,7 @@ "prepublish": "npm run build" }, "dependencies": { - "@libp2p/crypto": "^1.0.11", + "@libp2p/crypto": "^1.0.15", "@libp2p/interface-connection-encrypter": "^3.0.5", "@libp2p/interface-keys": "^1.0.6", "@libp2p/interface-metrics": "^4.0.4", @@ -76,9 +76,8 @@ "@libp2p/logger": "^2.0.5", "@libp2p/peer-id": "^2.0.0", "@stablelib/chacha20poly1305": "^1.0.1", - "@stablelib/hkdf": "^1.0.1", - "@stablelib/sha256": "^1.0.1", - "@stablelib/x25519": "^1.0.3", + "@noble/curves": "^1.0.0", + "@noble/hashes": "1.3.0", "it-length-prefixed": "^8.0.2", "it-pair": "^2.0.2", "it-pb-stream": "^3.2.0", diff --git a/src/crypto/stablelib.ts b/src/crypto/stablelib.ts index 13af52e9..63359170 100644 --- a/src/crypto/stablelib.ts +++ b/src/crypto/stablelib.ts @@ -1,6 +1,6 @@ -import { HKDF } from '@stablelib/hkdf' -import * as x25519 from '@stablelib/x25519' -import { SHA256, hash } from '@stablelib/sha256' +import { extract, expand } from '@noble/hashes/hkdf' +import { sha256 } from '@noble/hashes/sha256' +import { x25519 } from '@noble/curves/ed25519' import { ChaCha20Poly1305 } from '@stablelib/chacha20poly1305' import type { bytes32, bytes } from '../@types/basic.js' import type { Hkdf } from '../@types/handshake.js' @@ -9,12 +9,12 @@ import type { ICryptoInterface } from '../crypto.js' export const stablelib: ICryptoInterface = { hashSHA256 (data: Uint8Array): Uint8Array { - return hash(data) + return sha256(data) }, getHKDF (ck: bytes32, ikm: Uint8Array): Hkdf { - const hkdf = new HKDF(SHA256, ikm, ck) - const okmU8Array = hkdf.expand(96) + const prk = extract(sha256, ikm, ck) + const okmU8Array = expand(sha256, prk, undefined, 96) const okm = okmU8Array const k1 = okm.subarray(0, 32) @@ -25,25 +25,26 @@ export const stablelib: ICryptoInterface = { }, generateX25519KeyPair (): KeyPair { - const keypair = x25519.generateKeyPair() + const secretKey = x25519.utils.randomPrivateKey() + const publicKey = x25519.getPublicKey(secretKey) return { - publicKey: keypair.publicKey, - privateKey: keypair.secretKey + publicKey, + privateKey: secretKey } }, generateX25519KeyPairFromSeed (seed: Uint8Array): KeyPair { - const keypair = x25519.generateKeyPairFromSeed(seed) + const publicKey = x25519.getPublicKey(seed) return { - publicKey: keypair.publicKey, - privateKey: keypair.secretKey + publicKey, + privateKey: seed } }, generateX25519SharedKey (privateKey: Uint8Array, publicKey: Uint8Array): Uint8Array { - return x25519.sharedKey(privateKey, publicKey) + return x25519.getSharedSecret(privateKey, publicKey) }, chaCha20Poly1305Encrypt (plaintext: Uint8Array, nonce: Uint8Array, ad: Uint8Array, k: bytes32): bytes { From 0fb1b5b953970a913569fa1998a6ca25cb36af02 Mon Sep 17 00:00:00 2001 From: Marin Petrunic Date: Thu, 27 Apr 2023 10:25:10 +0200 Subject: [PATCH 2/3] update benchmark code Signed-off-by: Marin Petrunic --- benchmarks/benchmark.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/benchmarks/benchmark.js b/benchmarks/benchmark.js index d45c6d94..ed9c8d37 100644 --- a/benchmarks/benchmark.js +++ b/benchmarks/benchmark.js @@ -1,19 +1,19 @@ /* eslint-disable */ -import { Noise } from '../dist/src/index.js' +import { noise } from '../dist/src/index.js' import benchmark from 'benchmark' import { duplexPair } from 'it-pair/duplex' import { createFromJSON } from '@libp2p/peer-id-factory' const bench = async function () { console.log('Initializing handshake benchmark') - const initiator = new Noise() + const initiator = noise()() const initiatorPeer = await createFromJSON({ id: '12D3KooWH45PiqBjfnEfDfCD6TqJrpqTBJvQDwGHvjGpaWwms46D', privKey: 'CAESYBtKXrMwawAARmLScynQUuSwi/gGSkwqDPxi15N3dqDHa4T4iWupkMe5oYGwGH3Hyfvd/QcgSTqg71oYZJadJ6prhPiJa6mQx7mhgbAYfcfJ+939ByBJOqDvWhhklp0nqg==', pubKey: 'CAESIGuE+IlrqZDHuaGBsBh9x8n73f0HIEk6oO9aGGSWnSeq' }) - const responder = new Noise() + const responder = noise()() const responderPeer = await createFromJSON({ id: '12D3KooWP63uzL78BRMpkQ7augMdNi1h3VBrVWZucKjyhzGVaSi1', privKey: 'CAESYPxO3SHyfc2578hDmfkGGBY255JjiLuVavJWy+9ivlpsxSyVKf36ipyRGL6szGzHuFs5ceEuuGVrPMg/rW2Ch1bFLJUp/fqKnJEYvqzMbMe4Wzlx4S64ZWs8yD+tbYKHVg==', From f31f2759aa9bef689c2f7a315251cbdeec71233f Mon Sep 17 00:00:00 2001 From: Marin Petrunic Date: Mon, 17 Jul 2023 12:08:50 +0200 Subject: [PATCH 3/3] update noble ciphers Signed-off-by: Marin Petrunic --- package.json | 2 +- src/crypto/js.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 31bf0214..e4e7f5f3 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ "@libp2p/peer-id": "^2.0.0", "@noble/curves": "^1.1.0", "@noble/hashes": "^1.3.1", - "@noble/ciphers": "^0.1.3", + "@noble/ciphers": "^0.1.4", "it-length-prefixed": "^9.0.1", "it-pair": "^2.0.2", "it-pb-stream": "^4.0.1", diff --git a/src/crypto/js.ts b/src/crypto/js.ts index 50726c75..203de3ff 100644 --- a/src/crypto/js.ts +++ b/src/crypto/js.ts @@ -55,6 +55,7 @@ export const pureJsCrypto: ICryptoInterface = { const result = chacha20_poly1305(k, nonce, ad).decrypt(ciphertext) if (dst) { dst.set(result) + return result } return result }