From 6263eed8cfc30df7a4e0297695940558b54bb422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Mon, 4 Sep 2023 23:46:08 +0200 Subject: [PATCH] fix: reuse output when decrypting using noble/ciphers (#345) * reuse output when decrypting using noble/ciphers Signed-off-by: Marin Petrunic * bump @noble dep Signed-off-by: Marin Petrunic * update readme Signed-off-by: Marin Petrunic --------- Signed-off-by: Marin Petrunic --- .nvmrc | 2 +- README.md | 2 +- package.json | 2 +- src/crypto/js.ts | 11 +++-------- 4 files changed, 6 insertions(+), 11 deletions(-) diff --git a/.nvmrc b/.nvmrc index b6a7d89..3c03207 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -16 +18 diff --git a/README.md b/README.md index b7f2587..1acc4e3 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ This module exposes an implementation of the [ConnectionEncrypter](https://libp2 ## Bring your own crypto -You can provide a custom crypto implementation (instead of the default, based on [stablelib](https://www.stablelib.com/)) by adding a `crypto` field to the init argument passed to the `Noise` factory. +You can provide a custom crypto implementation (instead of the default, based on [@noble](https://paulmillr.com/noble/)) by adding a `crypto` field to the init argument passed to the `Noise` factory. The implementation must conform to the `ICryptoInterface`, defined in https://github.com/ChainSafe/js-libp2p-noise/blob/master/src/crypto.ts diff --git a/package.json b/package.json index 2d9653d..cf785ef 100644 --- a/package.json +++ b/package.json @@ -72,9 +72,9 @@ "@libp2p/interface": "^0.1.0", "@libp2p/logger": "^3.0.0", "@libp2p/peer-id": "^3.0.0", - "@noble/ciphers": "^0.1.4", "@noble/curves": "^1.1.0", "@noble/hashes": "^1.3.1", + "@noble/ciphers": "^0.3.0", "it-byte-stream": "^1.0.0", "it-length-prefixed": "^9.0.1", "it-length-prefixed-stream": "^1.0.0", diff --git a/src/crypto/js.ts b/src/crypto/js.ts index 203de3f..e47d476 100644 --- a/src/crypto/js.ts +++ b/src/crypto/js.ts @@ -1,4 +1,4 @@ -import { chacha20_poly1305 } from '@noble/ciphers/chacha' +import { chacha20poly1305 } from '@noble/ciphers/chacha' import { x25519 } from '@noble/curves/ed25519' import { extract, expand } from '@noble/hashes/hkdf' import { sha256 } from '@noble/hashes/sha256' @@ -48,15 +48,10 @@ export const pureJsCrypto: ICryptoInterface = { }, chaCha20Poly1305Encrypt (plaintext: Uint8Array, nonce: Uint8Array, ad: Uint8Array, k: bytes32): bytes { - return chacha20_poly1305(k, nonce, ad).encrypt(plaintext) + return chacha20poly1305(k, nonce, ad).encrypt(plaintext) }, chaCha20Poly1305Decrypt (ciphertext: Uint8Array, nonce: Uint8Array, ad: Uint8Array, k: bytes32, dst?: Uint8Array): bytes | null { - const result = chacha20_poly1305(k, nonce, ad).decrypt(ciphertext) - if (dst) { - dst.set(result) - return result - } - return result + return chacha20poly1305(k, nonce, ad).decrypt(ciphertext, dst) } }