Skip to content

Commit 6263eed

Browse files
authored
fix: reuse output when decrypting using noble/ciphers (#345)
* reuse output when decrypting using noble/ciphers Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com> * bump @noble dep Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com> * update readme Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com> --------- Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com>
1 parent c2e4dbb commit 6263eed

File tree

4 files changed

+6
-11
lines changed

4 files changed

+6
-11
lines changed

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
16
1+
18

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ This module exposes an implementation of the [ConnectionEncrypter](https://libp2
4747

4848
## Bring your own crypto
4949

50-
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.
50+
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.
5151

5252
The implementation must conform to the `ICryptoInterface`, defined in https://github.com/ChainSafe/js-libp2p-noise/blob/master/src/crypto.ts
5353

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@
7272
"@libp2p/interface": "^0.1.0",
7373
"@libp2p/logger": "^3.0.0",
7474
"@libp2p/peer-id": "^3.0.0",
75-
"@noble/ciphers": "^0.1.4",
7675
"@noble/curves": "^1.1.0",
7776
"@noble/hashes": "^1.3.1",
77+
"@noble/ciphers": "^0.3.0",
7878
"it-byte-stream": "^1.0.0",
7979
"it-length-prefixed": "^9.0.1",
8080
"it-length-prefixed-stream": "^1.0.0",

src/crypto/js.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { chacha20_poly1305 } from '@noble/ciphers/chacha'
1+
import { chacha20poly1305 } from '@noble/ciphers/chacha'
22
import { x25519 } from '@noble/curves/ed25519'
33
import { extract, expand } from '@noble/hashes/hkdf'
44
import { sha256 } from '@noble/hashes/sha256'
@@ -48,15 +48,10 @@ export const pureJsCrypto: ICryptoInterface = {
4848
},
4949

5050
chaCha20Poly1305Encrypt (plaintext: Uint8Array, nonce: Uint8Array, ad: Uint8Array, k: bytes32): bytes {
51-
return chacha20_poly1305(k, nonce, ad).encrypt(plaintext)
51+
return chacha20poly1305(k, nonce, ad).encrypt(plaintext)
5252
},
5353

5454
chaCha20Poly1305Decrypt (ciphertext: Uint8Array, nonce: Uint8Array, ad: Uint8Array, k: bytes32, dst?: Uint8Array): bytes | null {
55-
const result = chacha20_poly1305(k, nonce, ad).decrypt(ciphertext)
56-
if (dst) {
57-
dst.set(result)
58-
return result
59-
}
60-
return result
55+
return chacha20poly1305(k, nonce, ad).decrypt(ciphertext, dst)
6156
}
6257
}

0 commit comments

Comments
 (0)