Skip to content

Commit

Permalink
fix: reuse output when decrypting using noble/ciphers (#345)
Browse files Browse the repository at this point in the history
* reuse output when decrypting using noble/ciphers

Signed-off-by: Marin Petrunic <[email protected]>

* bump @noble dep

Signed-off-by: Marin Petrunic <[email protected]>

* update readme

Signed-off-by: Marin Petrunic <[email protected]>

---------

Signed-off-by: Marin Petrunic <[email protected]>
  • Loading branch information
mpetrunic authored Sep 4, 2023
1 parent c2e4dbb commit 6263eed
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16
18
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 3 additions & 8 deletions src/crypto/js.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 6263eed

Please sign in to comment.