libp2p-hs-0.1.0.0: Haskell implementation of the libp2p networking stack
Safe HaskellNone
LanguageGHC2021

LibP2P.Noise.Handshake

Description

Noise XX handshake for libp2p secure channels.

Implements the Noise_XX_25519_ChaChaPoly_SHA256 handshake pattern with libp2p-specific payload injection (identity key + signature).

Uses cacophony for the core Noise protocol state machine.

Synopsis

Handshake types

data HandshakeResult Source #

Result of a successful Noise handshake.

data NoisePayload Source #

Noise handshake payload (protobuf-encoded in messages 2 and 3).

Constructors

NoisePayload 

Fields

Instances

Instances details
Show NoisePayload Source # 
Instance details

Defined in LibP2P.Noise.Handshake

Eq NoisePayload Source # 
Instance details

Defined in LibP2P.Noise.Handshake

newtype HandshakeState Source #

Opaque handshake state wrapping cacophony's NoiseState.

Constructors

HandshakeState 

Fields

Payload encoding

encodeNoisePayload :: NoisePayload -> ByteString Source #

Encode a NoisePayload as a minimal protobuf message.

decodeNoisePayload :: ByteString -> Either String NoisePayload Source #

Decode a NoisePayload from protobuf bytes.

Protobuf does not constrain field order on the wire, so fields are dispatched by number in a loop rather than matched in a fixed sequence. Unknown fields are skipped according to their wire type (go-libp2p already emits an extensions message as field 4, and other implementations may add more). Both identity_key (field 1) and identity_sig (field 2) must be present; on duplicates the last occurrence wins, per protobuf merge semantics.

buildHandshakePayload :: KeyPair -> ByteString -> Either String NoisePayload Source #

Build a handshake payload from an identity key pair and Noise static pubkey.

Static key signing

signStaticKey :: PrivateKey -> ByteString -> Either String ByteString Source #

Sign the Noise static public key with the identity private key.

verifyStaticKey :: PublicKey -> ByteString -> ByteString -> Bool Source #

Verify a signature over the Noise static public key.

Handshake lifecycle

initHandshakeInitiator :: KeyPair -> IO (HandshakeState, ByteString) Source #

Initialize a handshake state for the initiator role. Returns (HandshakeState, noiseStaticPublicKey).

initHandshakeResponder :: KeyPair -> IO (HandshakeState, ByteString) Source #

Initialize a handshake state for the responder role. Returns (HandshakeState, noiseStaticPublicKey).

writeHandshakeMsg :: HandshakeState -> ByteString -> Either String (ByteString, HandshakeState) Source #

Write a handshake message with the given payload. Returns (ciphertext, updatedState).

readHandshakeMsg :: HandshakeState -> ByteString -> Either String (ByteString, HandshakeState) Source #

Read a handshake message and extract the decrypted payload. Returns (plaintext, updatedState).

sessionComplete :: HandshakeState -> Bool Source #

Check whether the handshake is complete.

Remote static key extraction

getRemoteNoiseStaticKey :: HandshakeState -> Maybe ByteString Source #

Extract the remote party's Noise static public key from the handshake state. Returns Just after the remote static key has been transmitted (msg2 for initiator, msg3 for responder in XX pattern).

Convenience

performFullHandshake :: KeyPair -> KeyPair -> IO (Either String (PeerId, PeerId)) Source #

Perform a full 3-message XX handshake between two peers. Returns the remote PeerId as seen by each side.

performFullHandshakeWithSessions :: KeyPair -> KeyPair -> IO (Either String (NoiseSession, NoiseSession)) Source #

Perform a full handshake and return transport sessions for both sides.

Re-exports for payload decoding

decodePublicKey :: ByteString -> Either String PublicKey Source #

Decode a protobuf-encoded PublicKey message.