Define functions which will be called when protocol messages are to be read and written to the remote peer.
The payloadIn and payloadOut functions are called when payloads are received and needed.
writeMsg :: ByteString -> IO ()
readMsg :: IO ByteString
payloadIn :: Plaintext -> IO ()
payloadOut :: IO Plaintext
-- If you don't need to use payloads, do the following:
let hc = HandshakeCallbacks (writeMsg socket)
(readMsg socket)
(\_ -> return ())
(return "")
Create the handshake state:
Select a handshake pattern to use. Patterns are defined in the Crypto.Noise.HandshakePatterns module.
Ensure that you provide the keys which are required by the handshake pattern you choose. For example,
the Noise_IK pattern requires that the initiator provides a local static key and a remote static key.
Remote keys are communicated out-of-band.
let hs = handshakeState $ HandshakeStateParams
noiseIK
""
-- ^ Prologue
(Just "foo")
-- ^ Pre-shared key
(Just initStatic)
-- ^ Local static key
Nothing
-- ^ Local ephemeral key
(Just (snd respStatic))
-- ^ Remote static key
Nothing
-- ^ Remote ephemeral key
True
-- ^ True if we are initiator
Run the handshake:
(encryptionCipherState, decryptionCipherState) <- runHandshake hs hc
Send and receive transport messages:
let (cipherText, encryptionCipherState') = encryptPayload "hello world" encryptionCipherState
let (Plaintext pt, decryptionCipherState') = decryptPayload msg decryptionCipherState
Ensure that you never re-use a cipher state with encryptPayload and decryptPayload.
Changes
0.5.0
Added Curve448 support
Major refactoring and API changes
A DSL was created to represent handshake patterns.
Added GHC 7.10.3 to unit tests
0.4.0
Improved documentation
Added basic benchmarks
Added better exception handling
Improved handshakeState API
Added psk2 functionality
Unit test cleanup
Renamed symmetricHandshake to symmetricState
Added BLAKE2, SHA512, AESGCM support
0.3.0
Brought API up to date with current version of spec (17)
0.2.0
Added support for one-way handshakes
Fixed Noise_IX
Added helper functions for ScrubbedBytes / ByteString conversion