module Pipes.Noise
(
InboundNoisePipe
, OutboundNoisePipe
, mkNoisePipes
) where
import Data.ByteString (ByteString)
import Pipes (Pipe, await, yield)
import Crypto.Noise
import Crypto.Noise.Cipher (Cipher)
import Crypto.Noise.DH (DH)
import Crypto.Noise.Hash (Hash)
type InboundNoisePipe = Pipe ByteString ScrubbedBytes
type OutboundNoisePipe = Pipe ScrubbedBytes ByteString
mkNoisePipes :: (Monad m, Cipher c, DH d, Hash h)
=> NoiseState c d h
-> Maybe (InboundNoisePipe m (NoiseResult c d h),
OutboundNoisePipe m (NoiseResult c d h))
mkNoisePipes ns | handshakeComplete ns = return (inboundPipe ns, outboundPipe ns)
| otherwise = Nothing
inboundPipe :: (Monad m, Cipher c, DH d, Hash h)
=> NoiseState c d h
-> InboundNoisePipe m (NoiseResult c d h)
inboundPipe ns = do
msg <- await
let result = readMessage (convert msg) ns
case result of
NoiseResultMessage pt ns' -> do
yield pt
inboundPipe ns'
NoiseResultNeedPSK _ -> return result
NoiseResultException _ -> return result
outboundPipe :: (Monad m, Cipher c, DH d, Hash h)
=> NoiseState c d h
-> OutboundNoisePipe m (NoiseResult c d h)
outboundPipe ns = do
msg <- await
let result = writeMessage msg ns
case result of
NoiseResultMessage ct ns' -> do
yield . convert $ ct
outboundPipe ns'
NoiseResultNeedPSK _ -> return result
NoiseResultException _ -> return result