{-# OPTIONS_HADDOCK prune #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
module Lightning.Protocol.BOLT8 (
Keypair
, keypair
, keypair_pub
, Pub
, parse_pub
, serialize_pub
, Handshake
, handshake_sender
, handshake_receiver
, handshake_remote_static
, Initiator
, act1
, act3
, Responder
, act2
, finalize
, Sender
, encrypt
, Receiver
, decrypt
, Pending
, decrypt_header
, decrypt_body
, Error(..)
) where
import Control.DeepSeq (NFData(..))
import Control.Monad (guard, unless)
import qualified Crypto.AEAD.ChaCha20Poly1305 as AEAD
import qualified Crypto.Curve.Secp256k1 as Secp256k1
import qualified Crypto.Hash.SHA256 as SHA256
import qualified Crypto.KDF.HMAC as HKDF
import Data.Bits (unsafeShiftR, (.&.))
import qualified Data.ByteString as BS
import qualified Data.ByteString.Unsafe as BU
import Data.Word (Word8, Word16, Word64)
import GHC.Generics (Generic)
newtype Sec = Sec BS.ByteString
newtype Pub = Pub Secp256k1.Projective
instance Eq Pub where
Pub Projective
a == :: Pub -> Pub -> Bool
== Pub Projective
b =
Projective -> ByteString
Secp256k1.serialize_point Projective
a ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== Projective -> ByteString
Secp256k1.serialize_point Projective
b
instance Show Pub where
showsPrec :: Int -> Pub -> ShowS
showsPrec Int
d Pub
p = Bool -> ShowS -> ShowS
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
String -> ShowS
showString String
"Pub " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
forall a. Show a => a -> ShowS
shows (ByteString -> String
hex (Pub -> ByteString
serialize_pub Pub
p))
instance NFData Pub where
rnf :: Pub -> ()
rnf (Pub Projective
p) = Projective
p Projective -> () -> ()
forall a b. a -> b -> b
`seq` ()
data Keypair = Keypair !Sec !Pub
instance NFData Keypair where
rnf :: Keypair -> ()
rnf (Keypair (Sec ByteString
s) Pub
p) = ByteString
s ByteString -> () -> ()
forall a b. a -> b -> b
`seq` Pub -> ()
forall a. NFData a => a -> ()
rnf Pub
p
keypair :: BS.ByteString -> Maybe Keypair
keypair :: ByteString -> Maybe Keypair
keypair ByteString
sec = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (ByteString -> Int
BS.length ByteString
sec Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
32)
k <- ByteString -> Maybe Wider
Secp256k1.parse_int256 ByteString
sec
p <- Secp256k1.derive_pub k
pure $! Keypair (Sec sec) (Pub p)
keypair_pub :: Keypair -> Pub
keypair_pub :: Keypair -> Pub
keypair_pub (Keypair Sec
_ Pub
p) = Pub
p
parse_pub :: BS.ByteString -> Maybe Pub
parse_pub :: ByteString -> Maybe Pub
parse_pub ByteString
bs = do
Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
33)
p <- ByteString -> Maybe Projective
Secp256k1.parse_point ByteString
bs
pure (Pub p)
serialize_pub :: Pub -> BS.ByteString
serialize_pub :: Pub -> ByteString
serialize_pub (Pub Projective
p) = Projective -> ByteString
Secp256k1.serialize_point Projective
p
data Initiator = Initiator
{-# UNPACK #-} !BS.ByteString
{-# UNPACK #-} !BS.ByteString
!Sec
!Keypair
!Pub
instance NFData Initiator where
rnf :: Initiator -> ()
rnf (Initiator ByteString
h ByteString
ck (Sec ByteString
e) Keypair
s Pub
rs) =
ByteString
h ByteString -> () -> ()
forall a b. a -> b -> b
`seq` ByteString
ck ByteString -> () -> ()
forall a b. a -> b -> b
`seq` ByteString
e ByteString -> () -> ()
forall a b. a -> b -> b
`seq` Keypair -> ()
forall a. NFData a => a -> ()
rnf Keypair
s () -> () -> ()
forall a b. a -> b -> b
`seq` Pub -> ()
forall a. NFData a => a -> ()
rnf Pub
rs
data Responder = Responder
{-# UNPACK #-} !BS.ByteString
{-# UNPACK #-} !BS.ByteString
{-# UNPACK #-} !BS.ByteString
!Sec
instance NFData Responder where
rnf :: Responder -> ()
rnf (Responder ByteString
h ByteString
ck ByteString
k (Sec ByteString
e)) = ByteString
h ByteString -> () -> ()
forall a b. a -> b -> b
`seq` ByteString
ck ByteString -> () -> ()
forall a b. a -> b -> b
`seq` ByteString
k ByteString -> () -> ()
forall a b. a -> b -> b
`seq` ByteString
e ByteString -> () -> ()
forall a b. a -> b -> b
`seq` ()
data Handshake = Handshake !Sender !Receiver !Pub
instance NFData Handshake where
rnf :: Handshake -> ()
rnf (Handshake Sender
s Receiver
r Pub
p) = Sender -> ()
forall a. NFData a => a -> ()
rnf Sender
s () -> () -> ()
forall a b. a -> b -> b
`seq` Receiver -> ()
forall a. NFData a => a -> ()
rnf Receiver
r () -> () -> ()
forall a b. a -> b -> b
`seq` Pub -> ()
forall a. NFData a => a -> ()
rnf Pub
p
handshake_sender :: Handshake -> Sender
handshake_sender :: Handshake -> Sender
handshake_sender (Handshake Sender
s Receiver
_ Pub
_) = Sender
s
handshake_receiver :: Handshake -> Receiver
handshake_receiver :: Handshake -> Receiver
handshake_receiver (Handshake Sender
_ Receiver
r Pub
_) = Receiver
r
handshake_remote_static :: Handshake -> Pub
handshake_remote_static :: Handshake -> Pub
handshake_remote_static (Handshake Sender
_ Receiver
_ Pub
p) = Pub
p
data Cipher = Cipher
{-# UNPACK #-} !BS.ByteString
{-# UNPACK #-} !Word64
{-# UNPACK #-} !BS.ByteString
instance NFData Cipher where
rnf :: Cipher -> ()
rnf (Cipher ByteString
k Word64
n ByteString
ck) = ByteString
k ByteString -> () -> ()
forall a b. a -> b -> b
`seq` Word64
n Word64 -> () -> ()
forall a b. a -> b -> b
`seq` ByteString
ck ByteString -> () -> ()
forall a b. a -> b -> b
`seq` ()
newtype Sender = Sender Cipher
instance NFData Sender where
rnf :: Sender -> ()
rnf (Sender Cipher
c) = Cipher -> ()
forall a. NFData a => a -> ()
rnf Cipher
c
newtype Receiver = Receiver Cipher
instance NFData Receiver where
rnf :: Receiver -> ()
rnf (Receiver Cipher
c) = Cipher -> ()
forall a. NFData a => a -> ()
rnf Cipher
c
data Pending = Pending {-# UNPACK #-} !Int !Cipher
instance NFData Pending where
rnf :: Pending -> ()
rnf (Pending Int
n Cipher
c) = Int
n Int -> () -> ()
forall a b. a -> b -> b
`seq` Cipher -> ()
forall a. NFData a => a -> ()
rnf Cipher
c
data Error =
InvalidEntropy
| InvalidLength
| InvalidVersion
| InvalidPub
| InvalidMAC
deriving (Error -> Error -> Bool
(Error -> Error -> Bool) -> (Error -> Error -> Bool) -> Eq Error
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Error -> Error -> Bool
== :: Error -> Error -> Bool
$c/= :: Error -> Error -> Bool
/= :: Error -> Error -> Bool
Eq, Int -> Error -> ShowS
[Error] -> ShowS
Error -> String
(Int -> Error -> ShowS)
-> (Error -> String) -> ([Error] -> ShowS) -> Show Error
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Error -> ShowS
showsPrec :: Int -> Error -> ShowS
$cshow :: Error -> String
show :: Error -> String
$cshowList :: [Error] -> ShowS
showList :: [Error] -> ShowS
Show, (forall x. Error -> Rep Error x)
-> (forall x. Rep Error x -> Error) -> Generic Error
forall x. Rep Error x -> Error
forall x. Error -> Rep Error x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Error -> Rep Error x
from :: forall x. Error -> Rep Error x
$cto :: forall x. Rep Error x -> Error
to :: forall x. Rep Error x -> Error
Generic)
instance NFData Error
_PROTOCOL_NAME :: BS.ByteString
_PROTOCOL_NAME :: ByteString
_PROTOCOL_NAME = ByteString
"Noise_XK_secp256k1_ChaChaPoly_SHA256"
_PROLOGUE :: BS.ByteString
_PROLOGUE :: ByteString
_PROLOGUE = ByteString
"lightning"
ecdh :: Sec -> Pub -> Maybe BS.ByteString
ecdh :: Sec -> Pub -> Maybe ByteString
ecdh (Sec ByteString
sec) (Pub Projective
pub) = do
k <- ByteString -> Maybe Wider
Secp256k1.parse_int256 ByteString
sec
pt <- Secp256k1.mul pub k
pure (SHA256.hash (Secp256k1.serialize_point pt))
mix_hash :: BS.ByteString -> BS.ByteString -> BS.ByteString
mix_hash :: ByteString -> ByteString -> ByteString
mix_hash ByteString
h ByteString
dat = ByteString -> ByteString
SHA256.hash (ByteString
h ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
dat)
mix_key
:: BS.ByteString
-> BS.ByteString
-> (BS.ByteString, BS.ByteString)
mix_key :: ByteString -> ByteString -> (ByteString, ByteString)
mix_key ByteString
ck ByteString
ikm =
case (ByteString -> ByteString -> ByteString)
-> ByteString
-> ByteString
-> Word64
-> ByteString
-> Maybe ByteString
HKDF.derive ByteString -> ByteString -> ByteString
hmac ByteString
ck ByteString
forall a. Monoid a => a
mempty Word64
64 ByteString
ikm of
Maybe ByteString
Nothing ->
String -> (ByteString, ByteString)
forall a. HasCallStack => String -> a
error String
"ppad-bolt8: internal error, please report a bug!"
Just ByteString
output -> Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
32 ByteString
output
where
hmac :: ByteString -> ByteString -> ByteString
hmac ByteString
k ByteString
b = case ByteString -> ByteString -> MAC
SHA256.hmac ByteString
k ByteString
b of
SHA256.MAC ByteString
mac -> ByteString
mac
encrypt_with_ad
:: BS.ByteString
-> Word64
-> BS.ByteString
-> BS.ByteString
-> BS.ByteString
encrypt_with_ad :: ByteString -> Word64 -> ByteString -> ByteString -> ByteString
encrypt_with_ad ByteString
key Word64
n ByteString
ad ByteString
pt =
case ByteString
-> ByteString
-> ByteString
-> ByteString
-> Either Error (ByteString, ByteString)
AEAD.encrypt ByteString
ad ByteString
key (Word64 -> ByteString
encode_nonce Word64
n) ByteString
pt of
Left Error
_ ->
String -> ByteString
forall a. HasCallStack => String -> a
error String
"ppad-bolt8: internal error, please report a bug!"
Right (ByteString
ct, ByteString
mac) -> ByteString
ct ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
mac
decrypt_with_ad
:: BS.ByteString
-> Word64
-> BS.ByteString
-> BS.ByteString
-> Maybe BS.ByteString
decrypt_with_ad :: ByteString
-> Word64 -> ByteString -> ByteString -> Maybe ByteString
decrypt_with_ad ByteString
key Word64
n ByteString
ad ByteString
ctmac
| ByteString -> Int
BS.length ByteString
ctmac Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
16 = Maybe ByteString
forall a. Maybe a
Nothing
| Bool
otherwise =
let (ByteString
ct, ByteString
mac) = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt (ByteString -> Int
BS.length ByteString
ctmac Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
16) ByteString
ctmac
in case ByteString
-> ByteString
-> ByteString
-> (ByteString, ByteString)
-> Either Error ByteString
AEAD.decrypt ByteString
ad ByteString
key (Word64 -> ByteString
encode_nonce Word64
n) (ByteString
ct, ByteString
mac) of
Left Error
_ -> Maybe ByteString
forall a. Maybe a
Nothing
Right ByteString
pt -> ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
pt
encode_nonce :: Word64 -> BS.ByteString
encode_nonce :: Word64 -> ByteString
encode_nonce Word64
n = Int -> Word8 -> ByteString
BS.replicate Int
4 Word8
0x00 ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> [Word8] -> ByteString
BS.pack [
Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi Word64
n
, Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftR Word64
n Int
8)
, Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftR Word64
n Int
16)
, Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftR Word64
n Int
24)
, Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftR Word64
n Int
32)
, Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftR Word64
n Int
40)
, Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftR Word64
n Int
48)
, Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
unsafeShiftR Word64
n Int
56)
]
encode_be16 :: Word16 -> BS.ByteString
encode_be16 :: Word16 -> ByteString
encode_be16 Word16
n = [Word8] -> ByteString
BS.pack [Word16 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word16 -> Int -> Word16
forall a. Bits a => a -> Int -> a
unsafeShiftR Word16
n Int
8), Word16 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi Word16
n]
decode_be16 :: BS.ByteString -> Maybe Word16
decode_be16 :: ByteString -> Maybe Word16
decode_be16 ByteString
bs
| ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
2 = Maybe Word16
forall a. Maybe a
Nothing
| Bool
otherwise =
let !b0 :: Word8
b0 = ByteString -> Int -> Word8
BU.unsafeIndex ByteString
bs Int
0
!b1 :: Word8
b1 = ByteString -> Int -> Word8
BU.unsafeIndex ByteString
bs Int
1
in Word16 -> Maybe Word16
forall a. a -> Maybe a
Just (Word8 -> Word16
forall a b. (Integral a, Num b) => a -> b
fi Word8
b0 Word16 -> Word16 -> Word16
forall a. Num a => a -> a -> a
* Word16
0x100 Word16 -> Word16 -> Word16
forall a. Num a => a -> a -> a
+ Word8 -> Word16
forall a b. (Integral a, Num b) => a -> b
fi Word8
b1)
initial_state :: Pub -> (BS.ByteString, BS.ByteString)
initial_state :: Pub -> (ByteString, ByteString)
initial_state Pub
rs =
let !h0 :: ByteString
h0 = ByteString -> ByteString
SHA256.hash ByteString
_PROTOCOL_NAME
!h1 :: ByteString
h1 = ByteString -> ByteString -> ByteString
mix_hash ByteString
h0 ByteString
_PROLOGUE
!h2 :: ByteString
h2 = ByteString -> ByteString -> ByteString
mix_hash ByteString
h1 (Pub -> ByteString
serialize_pub Pub
rs)
in (ByteString
h2, ByteString
h0)
parse_act :: BS.ByteString -> Either Error (BS.ByteString, BS.ByteString)
parse_act :: ByteString -> Either Error (ByteString, ByteString)
parse_act ByteString
msg = do
Bool -> Error -> Either Error ()
forall e. Bool -> e -> Either e ()
require (ByteString -> Int
BS.length ByteString
msg Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
50) Error
InvalidLength
Bool -> Error -> Either Error ()
forall e. Bool -> e -> Either e ()
require (ByteString -> Word8
BU.unsafeHead ByteString
msg Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x00) Error
InvalidVersion
let !(ByteString
e, ByteString
c) = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
33 (ByteString -> ByteString
BU.unsafeTail ByteString
msg)
(ByteString, ByteString) -> Either Error (ByteString, ByteString)
forall a. a -> Either Error a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString
e, ByteString
c)
act1
:: Keypair
-> Pub
-> BS.ByteString
-> Either Error (BS.ByteString, Initiator)
act1 :: Keypair
-> Pub -> ByteString -> Either Error (ByteString, Initiator)
act1 Keypair
s Pub
rs ByteString
ent = do
Keypair e_sec e_pub <- Error -> Maybe Keypair -> Either Error Keypair
forall e a. e -> Maybe a -> Either e a
note Error
InvalidEntropy (ByteString -> Maybe Keypair
keypair ByteString
ent)
let !(h0, ck0) = initial_state rs
!e_bytes = Pub -> ByteString
serialize_pub Pub
e_pub
!h1 = ByteString -> ByteString -> ByteString
mix_hash ByteString
h0 ByteString
e_bytes
es <- note InvalidPub (ecdh e_sec rs)
let !(ck1, temp_k1) = mix_key ck0 es
!c = ByteString -> Word64 -> ByteString -> ByteString -> ByteString
encrypt_with_ad ByteString
temp_k1 Word64
0 ByteString
h1 ByteString
BS.empty
!h2 = ByteString -> ByteString -> ByteString
mix_hash ByteString
h1 ByteString
c
!msg = Word8 -> ByteString -> ByteString
BS.cons Word8
0x00 (ByteString
e_bytes ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
c)
pure (msg, Initiator h2 ck1 e_sec s rs)
act2
:: Keypair
-> BS.ByteString
-> BS.ByteString
-> Either Error (BS.ByteString, Responder)
act2 :: Keypair
-> ByteString -> ByteString -> Either Error (ByteString, Responder)
act2 (Keypair Sec
s_sec Pub
s_pub) ByteString
ent ByteString
msg1 = do
(re_bytes, c) <- ByteString -> Either Error (ByteString, ByteString)
parse_act ByteString
msg1
re <- note InvalidPub (parse_pub re_bytes)
Keypair e_sec e_pub <- note InvalidEntropy (keypair ent)
let !(h0, ck0) = initial_state s_pub
!h1 = ByteString -> ByteString -> ByteString
mix_hash ByteString
h0 ByteString
re_bytes
es <- note InvalidPub (ecdh s_sec re)
let !(ck1, temp_k1) = mix_key ck0 es
_ <- note InvalidMAC (decrypt_with_ad temp_k1 0 h1 c)
let !h2 = ByteString -> ByteString -> ByteString
mix_hash ByteString
h1 ByteString
c
!e_bytes = Pub -> ByteString
serialize_pub Pub
e_pub
!h3 = ByteString -> ByteString -> ByteString
mix_hash ByteString
h2 ByteString
e_bytes
ee <- note InvalidPub (ecdh e_sec re)
let !(ck2, temp_k2) = mix_key ck1 ee
!c2 = ByteString -> Word64 -> ByteString -> ByteString -> ByteString
encrypt_with_ad ByteString
temp_k2 Word64
0 ByteString
h3 ByteString
BS.empty
!h4 = ByteString -> ByteString -> ByteString
mix_hash ByteString
h3 ByteString
c2
!msg = Word8 -> ByteString -> ByteString
BS.cons Word8
0x00 (ByteString
e_bytes ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
c2)
pure (msg, Responder h4 ck2 temp_k2 e_sec)
act3
:: Initiator
-> BS.ByteString
-> Either Error (BS.ByteString, Handshake)
act3 :: Initiator -> ByteString -> Either Error (ByteString, Handshake)
act3 (Initiator ByteString
h ByteString
ck Sec
e_sec (Keypair Sec
s_sec Pub
s_pub) Pub
rs) ByteString
msg2 = do
(re_bytes, c) <- ByteString -> Either Error (ByteString, ByteString)
parse_act ByteString
msg2
re <- note InvalidPub (parse_pub re_bytes)
let !h1 = ByteString -> ByteString -> ByteString
mix_hash ByteString
h ByteString
re_bytes
ee <- note InvalidPub (ecdh e_sec re)
let !(ck1, temp_k2) = mix_key ck ee
_ <- note InvalidMAC (decrypt_with_ad temp_k2 0 h1 c)
let !h2 = ByteString -> ByteString -> ByteString
mix_hash ByteString
h1 ByteString
c
!c3 = ByteString -> Word64 -> ByteString -> ByteString -> ByteString
encrypt_with_ad ByteString
temp_k2 Word64
1 ByteString
h2 (Pub -> ByteString
serialize_pub Pub
s_pub)
!h3 = ByteString -> ByteString -> ByteString
mix_hash ByteString
h2 ByteString
c3
se <- note InvalidPub (ecdh s_sec re)
let !(ck2, temp_k3) = mix_key ck1 se
!t = ByteString -> Word64 -> ByteString -> ByteString -> ByteString
encrypt_with_ad ByteString
temp_k3 Word64
0 ByteString
h3 ByteString
BS.empty
!(sk, rk) = mix_key ck2 BS.empty
!msg = Word8 -> ByteString -> ByteString
BS.cons Word8
0x00 (ByteString
c3 ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
t)
!hs = Sender -> Receiver -> Pub -> Handshake
Handshake (Cipher -> Sender
Sender (ByteString -> Word64 -> ByteString -> Cipher
Cipher ByteString
sk Word64
0 ByteString
ck2))
(Cipher -> Receiver
Receiver (ByteString -> Word64 -> ByteString -> Cipher
Cipher ByteString
rk Word64
0 ByteString
ck2)) Pub
rs
pure (msg, hs)
finalize
:: Responder
-> BS.ByteString
-> Either Error Handshake
finalize :: Responder -> ByteString -> Either Error Handshake
finalize (Responder ByteString
h ByteString
ck ByteString
temp_k2 Sec
e_sec) ByteString
msg3 = do
Bool -> Error -> Either Error ()
forall e. Bool -> e -> Either e ()
require (ByteString -> Int
BS.length ByteString
msg3 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
66) Error
InvalidLength
Bool -> Error -> Either Error ()
forall e. Bool -> e -> Either e ()
require (ByteString -> Word8
BU.unsafeHead ByteString
msg3 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x00) Error
InvalidVersion
let !(ByteString
c, ByteString
t) = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
49 (ByteString -> ByteString
BU.unsafeTail ByteString
msg3)
rs_bytes <- Error -> Maybe ByteString -> Either Error ByteString
forall e a. e -> Maybe a -> Either e a
note Error
InvalidMAC (ByteString
-> Word64 -> ByteString -> ByteString -> Maybe ByteString
decrypt_with_ad ByteString
temp_k2 Word64
1 ByteString
h ByteString
c)
rs <- note InvalidPub (parse_pub rs_bytes)
let !h1 = ByteString -> ByteString -> ByteString
mix_hash ByteString
h ByteString
c
se <- note InvalidPub (ecdh e_sec rs)
let !(ck1, temp_k3) = mix_key ck se
_ <- note InvalidMAC (decrypt_with_ad temp_k3 0 h1 t)
let !(rk, sk) = mix_key ck1 BS.empty
pure (Handshake (Sender (Cipher sk 0 ck1)) (Receiver (Cipher rk 0 ck1)) rs)
seal :: Cipher -> BS.ByteString -> (BS.ByteString, Cipher)
seal :: Cipher -> ByteString -> (ByteString, Cipher)
seal c :: Cipher
c@(Cipher ByteString
k Word64
n ByteString
_) ByteString
pt =
let !ct :: ByteString
ct = ByteString -> Word64 -> ByteString -> ByteString -> ByteString
encrypt_with_ad ByteString
k Word64
n ByteString
BS.empty ByteString
pt
in (ByteString
ct, Cipher -> Cipher
step Cipher
c)
open :: Cipher -> BS.ByteString -> Maybe (BS.ByteString, Cipher)
open :: Cipher -> ByteString -> Maybe (ByteString, Cipher)
open c :: Cipher
c@(Cipher ByteString
k Word64
n ByteString
_) ByteString
ct = do
pt <- ByteString
-> Word64 -> ByteString -> ByteString -> Maybe ByteString
decrypt_with_ad ByteString
k Word64
n ByteString
BS.empty ByteString
ct
pure (pt, step c)
step :: Cipher -> Cipher
step :: Cipher -> Cipher
step (Cipher ByteString
k Word64
n ByteString
ck)
| Word64
n' Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
1000 =
let !(ByteString
ck', ByteString
k') = ByteString -> ByteString -> (ByteString, ByteString)
mix_key ByteString
ck ByteString
k
in ByteString -> Word64 -> ByteString -> Cipher
Cipher ByteString
k' Word64
0 ByteString
ck'
| Bool
otherwise = ByteString -> Word64 -> ByteString -> Cipher
Cipher ByteString
k Word64
n' ByteString
ck
where
!n' :: Word64
n' = Word64
n Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
1
encrypt
:: Sender
-> BS.ByteString
-> Either Error (BS.ByteString, Sender)
encrypt :: Sender -> ByteString -> Either Error (ByteString, Sender)
encrypt (Sender Cipher
c0) ByteString
m = do
let !len :: Int
len = ByteString -> Int
BS.length ByteString
m
Bool -> Error -> Either Error ()
forall e. Bool -> e -> Either e ()
require (Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
65535) Error
InvalidLength
let !(ByteString
lc, Cipher
c1) = Cipher -> ByteString -> (ByteString, Cipher)
seal Cipher
c0 (Word16 -> ByteString
encode_be16 (Int -> Word16
forall a b. (Integral a, Num b) => a -> b
fi Int
len))
!(ByteString
bc, Cipher
c2) = Cipher -> ByteString -> (ByteString, Cipher)
seal Cipher
c1 ByteString
m
(ByteString, Sender) -> Either Error (ByteString, Sender)
forall a. a -> Either Error a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString
lc ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
bc, Cipher -> Sender
Sender Cipher
c2)
decrypt
:: Receiver
-> BS.ByteString
-> Either Error (BS.ByteString, Receiver)
decrypt :: Receiver -> ByteString -> Either Error (ByteString, Receiver)
decrypt Receiver
r ByteString
frame = do
let !(ByteString
lc, ByteString
c) = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
18 ByteString
frame
(_, p) <- Receiver -> ByteString -> Either Error (Int, Pending)
decrypt_header Receiver
r ByteString
lc
decrypt_body p c
decrypt_header
:: Receiver
-> BS.ByteString
-> Either Error (Int, Pending)
(Receiver Cipher
c0) ByteString
lc = do
Bool -> Error -> Either Error ()
forall e. Bool -> e -> Either e ()
require (ByteString -> Int
BS.length ByteString
lc Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
18) Error
InvalidLength
(l, c1) <- Error
-> Maybe (ByteString, Cipher) -> Either Error (ByteString, Cipher)
forall e a. e -> Maybe a -> Either e a
note Error
InvalidMAC (Cipher -> ByteString -> Maybe (ByteString, Cipher)
open Cipher
c0 ByteString
lc)
len <- note InvalidLength (decode_be16 l)
let !body = Word16 -> Int
forall a b. (Integral a, Num b) => a -> b
fi Word16
len Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
16
pure (body, Pending body c1)
decrypt_body
:: Pending
-> BS.ByteString
-> Either Error (BS.ByteString, Receiver)
decrypt_body :: Pending -> ByteString -> Either Error (ByteString, Receiver)
decrypt_body (Pending Int
body Cipher
c1) ByteString
c = do
Bool -> Error -> Either Error ()
forall e. Bool -> e -> Either e ()
require (ByteString -> Int
BS.length ByteString
c Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
body) Error
InvalidLength
(m, c2) <- Error
-> Maybe (ByteString, Cipher) -> Either Error (ByteString, Cipher)
forall e a. e -> Maybe a -> Either e a
note Error
InvalidMAC (Cipher -> ByteString -> Maybe (ByteString, Cipher)
open Cipher
c1 ByteString
c)
pure (m, Receiver c2)
hex :: BS.ByteString -> String
hex :: ByteString -> String
hex = (Word8 -> ShowS) -> String -> ByteString -> String
forall a. (Word8 -> a -> a) -> a -> ByteString -> a
BS.foldr (\Word8
w String
acc -> Word8 -> Char
nibble (Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
unsafeShiftR Word8
w Int
4) Char -> ShowS
forall a. a -> [a] -> [a]
: Word8 -> Char
nibble (Word8
w Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
15) Char -> ShowS
forall a. a -> [a] -> [a]
: String
acc)
[]
where
nibble :: Word8 -> Char
nibble :: Word8 -> Char
nibble Word8
n
| Word8
n Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
< Word8
10 = Int -> Char
forall a. Enum a => Int -> a
toEnum (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fi Word8
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
0x30)
| Bool
otherwise = Int -> Char
forall a. Enum a => Int -> a
toEnum (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fi Word8
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
0x57)
note :: e -> Maybe a -> Either e a
note :: forall e a. e -> Maybe a -> Either e a
note e
e = Either e a -> (a -> Either e a) -> Maybe a -> Either e a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (e -> Either e a
forall a b. a -> Either a b
Left e
e) a -> Either e a
forall a b. b -> Either a b
Right
{-# INLINE note #-}
require :: Bool -> e -> Either e ()
require :: forall e. Bool -> e -> Either e ()
require Bool
cond e
e = Bool -> Either e () -> Either e ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
cond (e -> Either e ()
forall a b. a -> Either a b
Left e
e)
{-# INLINE require #-}
fi :: (Integral a, Num b) => a -> b
fi :: forall a b. (Integral a, Num b) => a -> b
fi = a -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINE fi #-}