@chainsafe/libp2p-noise
Advanced tools
Comparing version 10.1.0 to 10.2.0
@@ -1,5 +0,5 @@ | ||
export declare type bytes = Uint8Array; | ||
export declare type bytes32 = Uint8Array; | ||
export declare type bytes16 = Uint8Array; | ||
export declare type uint64 = number; | ||
export type bytes = Uint8Array; | ||
export type bytes32 = Uint8Array; | ||
export type bytes16 = Uint8Array; | ||
export type uint64 = number; | ||
//# sourceMappingURL=basic.d.ts.map |
@@ -10,3 +10,3 @@ import type { PeerId } from '@libp2p/interface-peer-id'; | ||
encrypt: (plaintext: bytes, session: NoiseSession) => bytes; | ||
decrypt: (ciphertext: bytes, session: NoiseSession) => { | ||
decrypt: (ciphertext: bytes, session: NoiseSession, dst?: Uint8Array) => { | ||
plaintext: bytes; | ||
@@ -13,0 +13,0 @@ valid: boolean; |
import type { bytes, bytes32, uint64 } from './basic.js'; | ||
import type { KeyPair } from './libp2p.js'; | ||
import type { Nonce } from '../nonce.js'; | ||
export declare type Hkdf = [bytes, bytes, bytes]; | ||
export type Hkdf = [bytes, bytes, bytes]; | ||
export interface MessageBuffer { | ||
@@ -6,0 +6,0 @@ ne: bytes32; |
@@ -11,4 +11,4 @@ import type { bytes32, bytes } from './@types/basic.js'; | ||
chaCha20Poly1305Encrypt: (plaintext: Uint8Array, nonce: Uint8Array, ad: Uint8Array, k: bytes32) => bytes; | ||
chaCha20Poly1305Decrypt: (ciphertext: Uint8Array, nonce: Uint8Array, ad: Uint8Array, k: bytes32) => bytes | null; | ||
chaCha20Poly1305Decrypt: (ciphertext: Uint8Array, nonce: Uint8Array, ad: Uint8Array, k: bytes32, dst?: Uint8Array) => bytes | null; | ||
} | ||
//# sourceMappingURL=crypto.d.ts.map |
@@ -39,7 +39,7 @@ import { HKDF } from '@stablelib/hkdf'; | ||
}, | ||
chaCha20Poly1305Decrypt(ciphertext, nonce, ad, k) { | ||
chaCha20Poly1305Decrypt(ciphertext, nonce, ad, k, dst) { | ||
const ctx = new ChaCha20Poly1305(k); | ||
return ctx.open(nonce, ciphertext, ad); | ||
return ctx.open(nonce, ciphertext, ad, dst); | ||
} | ||
}; | ||
//# sourceMappingURL=stablelib.js.map |
@@ -0,1 +1,2 @@ | ||
import { TAG_LENGTH } from '@stablelib/chacha20poly1305'; | ||
import { NOISE_MSG_MAX_LENGTH_BYTES, NOISE_MSG_MAX_LENGTH_BYTES_WITHOUT_TAG } from '../constants.js'; | ||
@@ -29,3 +30,12 @@ import { uint16BEEncode } from '../encoder.js'; | ||
} | ||
const { plaintext: decrypted, valid } = handshake.decrypt(chunk.subarray(i, end), handshake.session); | ||
if (end - TAG_LENGTH < i) { | ||
throw new Error('Invalid chunk'); | ||
} | ||
const encrypted = chunk.subarray(i, end); | ||
// memory allocation is not cheap so reuse the encrypted Uint8Array | ||
// see https://github.com/ChainSafe/js-libp2p-noise/pull/242#issue-1422126164 | ||
// this is ok because chacha20 reads bytes one by one and don't reread after that | ||
// it's also tested in https://github.com/ChainSafe/as-chacha20poly1305/pull/1/files#diff-25252846b58979dcaf4e41d47b3eadd7e4f335e7fb98da6c049b1f9cd011f381R48 | ||
const dst = chunk.subarray(i, end - TAG_LENGTH); | ||
const { plaintext: decrypted, valid } = handshake.decrypt(encrypted, handshake.session, dst); | ||
if (!valid) { | ||
@@ -32,0 +42,0 @@ metrics?.decryptErrors.increment(); |
@@ -25,3 +25,3 @@ import type { PeerId } from '@libp2p/interface-peer-id'; | ||
encrypt(plaintext: Uint8Array, session: NoiseSession): bytes; | ||
decrypt(ciphertext: Uint8Array, session: NoiseSession): { | ||
decrypt(ciphertext: Uint8Array, session: NoiseSession, dst?: Uint8Array): { | ||
plaintext: bytes; | ||
@@ -28,0 +28,0 @@ valid: boolean; |
@@ -107,5 +107,5 @@ import { InvalidCryptoExchangeError, UnexpectedPeerError } from '@libp2p/interface-connection-encrypter/errors'; | ||
} | ||
decrypt(ciphertext, session) { | ||
decrypt(ciphertext, session, dst) { | ||
const cs = this.getCS(session, false); | ||
return this.xx.decryptWithAd(cs, new Uint8Array(0), ciphertext); | ||
return this.xx.decryptWithAd(cs, new Uint8Array(0), ciphertext, dst); | ||
} | ||
@@ -112,0 +112,0 @@ getRemoteStaticKey() { |
@@ -9,3 +9,3 @@ import type { bytes, bytes32 } from '../@types/basic.js'; | ||
encryptWithAd(cs: CipherState, ad: Uint8Array, plaintext: Uint8Array): bytes; | ||
decryptWithAd(cs: CipherState, ad: Uint8Array, ciphertext: Uint8Array): { | ||
decryptWithAd(cs: CipherState, ad: Uint8Array, ciphertext: Uint8Array, dst?: Uint8Array): { | ||
plaintext: bytes; | ||
@@ -19,3 +19,3 @@ valid: boolean; | ||
protected encryptAndHash(ss: SymmetricState, plaintext: bytes): bytes; | ||
protected decrypt(k: bytes32, n: Nonce, ad: bytes, ciphertext: bytes): { | ||
protected decrypt(k: bytes32, n: Nonce, ad: bytes, ciphertext: bytes, dst?: Uint8Array): { | ||
plaintext: bytes; | ||
@@ -22,0 +22,0 @@ valid: boolean; |
@@ -15,4 +15,4 @@ import { equals as uint8ArrayEquals } from 'uint8arrays/equals'; | ||
} | ||
decryptWithAd(cs, ad, ciphertext) { | ||
const { plaintext, valid } = this.decrypt(cs.k, cs.n, ad, ciphertext); | ||
decryptWithAd(cs, ad, ciphertext, dst) { | ||
const { plaintext, valid } = this.decrypt(cs.k, cs.n, ad, ciphertext, dst); | ||
if (valid) | ||
@@ -48,5 +48,5 @@ cs.n.increment(); | ||
} | ||
decrypt(k, n, ad, ciphertext) { | ||
decrypt(k, n, ad, ciphertext, dst) { | ||
n.assertValue(); | ||
const encryptedMessage = this.crypto.chaCha20Poly1305Decrypt(ciphertext, n.getBytes(), ad, k); | ||
const encryptedMessage = this.crypto.chaCha20Poly1305Decrypt(ciphertext, n.getBytes(), ad, k, dst); | ||
if (encryptedMessage) { | ||
@@ -53,0 +53,0 @@ return { |
import type { Metrics } from '@libp2p/interface-metrics'; | ||
export declare type MetricsRegistry = ReturnType<typeof registerMetrics>; | ||
export type MetricsRegistry = ReturnType<typeof registerMetrics>; | ||
export declare function registerMetrics(metrics: Metrics): { | ||
@@ -4,0 +4,0 @@ xxHandshakeSuccesses: import("@libp2p/interface-metrics").Counter; |
{ | ||
"name": "@chainsafe/libp2p-noise", | ||
"version": "10.1.0", | ||
"version": "10.2.0", | ||
"author": "ChainSafe <info@chainsafe.io>", | ||
@@ -97,3 +97,3 @@ "license": "Apache-2.0 OR MIT", | ||
"@libp2p/peer-id-factory": "^1.0.8", | ||
"@libp2p/tcp": "^5.0.1", | ||
"@libp2p/tcp": "^6.0.2", | ||
"@multiformats/multiaddr": "^11.0.3", | ||
@@ -100,0 +100,0 @@ "aegir": "^37.3.0", |
@@ -11,3 +11,3 @@ import type { PeerId } from '@libp2p/interface-peer-id' | ||
encrypt: (plaintext: bytes, session: NoiseSession) => bytes | ||
decrypt: (ciphertext: bytes, session: NoiseSession) => { plaintext: bytes, valid: boolean } | ||
decrypt: (ciphertext: bytes, session: NoiseSession, dst?: Uint8Array) => { plaintext: bytes, valid: boolean } | ||
} |
@@ -15,3 +15,3 @@ import type { bytes32, bytes } from './@types/basic.js' | ||
chaCha20Poly1305Encrypt: (plaintext: Uint8Array, nonce: Uint8Array, ad: Uint8Array, k: bytes32) => bytes | ||
chaCha20Poly1305Decrypt: (ciphertext: Uint8Array, nonce: Uint8Array, ad: Uint8Array, k: bytes32) => bytes | null | ||
chaCha20Poly1305Decrypt: (ciphertext: Uint8Array, nonce: Uint8Array, ad: Uint8Array, k: bytes32, dst?: Uint8Array) => bytes | null | ||
} |
@@ -55,7 +55,7 @@ import { HKDF } from '@stablelib/hkdf' | ||
chaCha20Poly1305Decrypt (ciphertext: Uint8Array, nonce: Uint8Array, ad: Uint8Array, k: bytes32): bytes | null { | ||
chaCha20Poly1305Decrypt (ciphertext: Uint8Array, nonce: Uint8Array, ad: Uint8Array, k: bytes32, dst?: Uint8Array): bytes | null { | ||
const ctx = new ChaCha20Poly1305(k) | ||
return ctx.open(nonce, ciphertext, ad) | ||
return ctx.open(nonce, ciphertext, ad, dst) | ||
} | ||
} |
@@ -0,1 +1,2 @@ | ||
import { TAG_LENGTH } from '@stablelib/chacha20poly1305' | ||
import type { Transform } from 'it-stream-types' | ||
@@ -38,3 +39,12 @@ import type { Uint8ArrayList } from 'uint8arraylist' | ||
const { plaintext: decrypted, valid } = handshake.decrypt(chunk.subarray(i, end), handshake.session) | ||
if (end - TAG_LENGTH < i) { | ||
throw new Error('Invalid chunk') | ||
} | ||
const encrypted = chunk.subarray(i, end) | ||
// memory allocation is not cheap so reuse the encrypted Uint8Array | ||
// see https://github.com/ChainSafe/js-libp2p-noise/pull/242#issue-1422126164 | ||
// this is ok because chacha20 reads bytes one by one and don't reread after that | ||
// it's also tested in https://github.com/ChainSafe/as-chacha20poly1305/pull/1/files#diff-25252846b58979dcaf4e41d47b3eadd7e4f335e7fb98da6c049b1f9cd011f381R48 | ||
const dst = chunk.subarray(i, end - TAG_LENGTH) | ||
const { plaintext: decrypted, valid } = handshake.decrypt(encrypted, handshake.session, dst) | ||
if (!valid) { | ||
@@ -41,0 +51,0 @@ metrics?.decryptErrors.increment() |
@@ -150,6 +150,6 @@ import type { PeerId } from '@libp2p/interface-peer-id' | ||
public decrypt (ciphertext: Uint8Array, session: NoiseSession): { plaintext: bytes, valid: boolean } { | ||
public decrypt (ciphertext: Uint8Array, session: NoiseSession, dst?: Uint8Array): { plaintext: bytes, valid: boolean } { | ||
const cs = this.getCS(session, false) | ||
return this.xx.decryptWithAd(cs, new Uint8Array(0), ciphertext) | ||
return this.xx.decryptWithAd(cs, new Uint8Array(0), ciphertext, dst) | ||
} | ||
@@ -156,0 +156,0 @@ |
@@ -24,4 +24,4 @@ import { equals as uint8ArrayEquals } from 'uint8arrays/equals' | ||
public decryptWithAd (cs: CipherState, ad: Uint8Array, ciphertext: Uint8Array): {plaintext: bytes, valid: boolean} { | ||
const { plaintext, valid } = this.decrypt(cs.k, cs.n, ad, ciphertext) | ||
public decryptWithAd (cs: CipherState, ad: Uint8Array, ciphertext: Uint8Array, dst?: Uint8Array): {plaintext: bytes, valid: boolean} { | ||
const { plaintext, valid } = this.decrypt(cs.k, cs.n, ad, ciphertext, dst) | ||
if (valid) cs.n.increment() | ||
@@ -64,6 +64,6 @@ | ||
protected decrypt (k: bytes32, n: Nonce, ad: bytes, ciphertext: bytes): {plaintext: bytes, valid: boolean} { | ||
protected decrypt (k: bytes32, n: Nonce, ad: bytes, ciphertext: bytes, dst?: Uint8Array): {plaintext: bytes, valid: boolean} { | ||
n.assertValue() | ||
const encryptedMessage = this.crypto.chaCha20Poly1305Decrypt(ciphertext, n.getBytes(), ad, k) | ||
const encryptedMessage = this.crypto.chaCha20Poly1305Decrypt(ciphertext, n.getBytes(), ad, k, dst) | ||
@@ -70,0 +70,0 @@ if (encryptedMessage) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
164851
2653