{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveGeneric #-}
module Lightning.Protocol.BOLT4.Construct (
Hop(..)
, ConstructError(..)
, construct
) where
import Control.DeepSeq (NFData)
import qualified Crypto.Curve.Secp256k1 as Secp256k1
import qualified Data.ByteString as BS
import qualified Data.List as L
import GHC.Generics (Generic)
import qualified Lightning.Protocol.BOLT1 as BOLT1
import Lightning.Protocol.BOLT4.Codec (encode_hop_payload)
import Lightning.Protocol.BOLT4.Prim
import Lightning.Protocol.BOLT4.Types
data Hop = Hop
{ Hop -> Point
hop_pubkey :: !BOLT1.Point
, Hop -> HopPayload
hop_payload :: !HopPayload
} deriving (Hop -> Hop -> Bool
(Hop -> Hop -> Bool) -> (Hop -> Hop -> Bool) -> Eq Hop
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Hop -> Hop -> Bool
== :: Hop -> Hop -> Bool
$c/= :: Hop -> Hop -> Bool
/= :: Hop -> Hop -> Bool
Eq, Int -> Hop -> ShowS
[Hop] -> ShowS
Hop -> String
(Int -> Hop -> ShowS)
-> (Hop -> String) -> ([Hop] -> ShowS) -> Show Hop
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Hop -> ShowS
showsPrec :: Int -> Hop -> ShowS
$cshow :: Hop -> String
show :: Hop -> String
$cshowList :: [Hop] -> ShowS
showList :: [Hop] -> ShowS
Show, (forall x. Hop -> Rep Hop x)
-> (forall x. Rep Hop x -> Hop) -> Generic Hop
forall x. Rep Hop x -> Hop
forall x. Hop -> Rep Hop x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Hop -> Rep Hop x
from :: forall x. Hop -> Rep Hop x
$cto :: forall x. Rep Hop x -> Hop
to :: forall x. Rep Hop x -> Hop
Generic)
instance NFData Hop
data ConstructError
= EmptyRoute
| TooManyHops
| InvalidHopPubKey {-# UNPACK #-} !Int
| InvalidHopPayload {-# UNPACK #-} !Int
| PayloadsTooLarge {-# UNPACK #-} !Int
deriving (ConstructError -> ConstructError -> Bool
(ConstructError -> ConstructError -> Bool)
-> (ConstructError -> ConstructError -> Bool) -> Eq ConstructError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ConstructError -> ConstructError -> Bool
== :: ConstructError -> ConstructError -> Bool
$c/= :: ConstructError -> ConstructError -> Bool
/= :: ConstructError -> ConstructError -> Bool
Eq, Int -> ConstructError -> ShowS
[ConstructError] -> ShowS
ConstructError -> String
(Int -> ConstructError -> ShowS)
-> (ConstructError -> String)
-> ([ConstructError] -> ShowS)
-> Show ConstructError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ConstructError -> ShowS
showsPrec :: Int -> ConstructError -> ShowS
$cshow :: ConstructError -> String
show :: ConstructError -> String
$cshowList :: [ConstructError] -> ShowS
showList :: [ConstructError] -> ShowS
Show, (forall x. ConstructError -> Rep ConstructError x)
-> (forall x. Rep ConstructError x -> ConstructError)
-> Generic ConstructError
forall x. Rep ConstructError x -> ConstructError
forall x. ConstructError -> Rep ConstructError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ConstructError -> Rep ConstructError x
from :: forall x. ConstructError -> Rep ConstructError x
$cto :: forall x. Rep ConstructError x -> ConstructError
to :: forall x. Rep ConstructError x -> ConstructError
Generic)
instance NFData ConstructError
construct
:: SecretKey
-> [Hop]
-> BS.ByteString
-> Either ConstructError (OnionPacket, [SharedSecret])
construct :: SecretKey
-> [Hop]
-> ByteString
-> Either ConstructError (OnionPacket, [SharedSecret])
construct SecretKey
sk [Hop]
hops ByteString
ad
| [Hop] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Hop]
hops = ConstructError
-> Either ConstructError (OnionPacket, [SharedSecret])
forall a b. a -> Either a b
Left ConstructError
EmptyRoute
| [Hop] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Hop]
hops Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
20 = ConstructError
-> Either ConstructError (OnionPacket, [SharedSecret])
forall a b. a -> Either a b
Left ConstructError
TooManyHops
| Bool
otherwise = do
pubs <- ((Int, Hop) -> Either ConstructError Projective)
-> [(Int, Hop)] -> Either ConstructError [Projective]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Int, Hop) -> Either ConstructError Projective
parse_pub [(Int, Hop)]
ihops
pls <- traverse encode_pl ihops
let sizes = (ByteString -> Int) -> [ByteString] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map ByteString -> Int
shift_size [ByteString]
pls
total = [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Int]
sizes
if total > 1300
then Left (PayloadsTooLarge total)
else do
sss <- shared_secrets (sk_bytes sk) (sk_pub sk) (zip [0 ..] pubs)
let streams = (SharedSecret -> ByteString) -> [SharedSecret] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (\SharedSecret
ss -> DerivedKey -> Int -> ByteString
keystream (SharedSecret -> DerivedKey
derive_rho SharedSecret
ss) Int
2600) [SharedSecret]
sss
n = [Hop] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Hop]
hops
fill = [ByteString] -> [Int] -> ByteString
filler (Int -> [ByteString] -> [ByteString]
forall a. Int -> [a] -> [a]
take (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [ByteString]
streams) (Int -> [Int] -> [Int]
forall a. Int -> [a] -> [a]
take (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) [Int]
sizes)
start = DerivedKey -> Int -> ByteString
keystream (SecretKey -> DerivedKey
derive_pad SecretKey
sk) Int
1300
(body, mac) = wrap ad fill start
(reverse (zip3 sss streams pls))
pkt = Word8 -> Point -> HopPayloads -> Hmac32 -> OnionPacket
OnionPacket Word8
0 (SecretKey -> Point
public_key SecretKey
sk) (ByteString -> HopPayloads
HopPayloads ByteString
body)
(ByteString -> Hmac32
Hmac32 ByteString
mac)
pure (pkt, sss)
where
ihops :: [(Int, Hop)]
ihops = [Int] -> [Hop] -> [(Int, Hop)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0 ..] [Hop]
hops
parse_pub :: (Int, Hop) -> Either ConstructError Projective
parse_pub (Int
i, Hop
h) =
Either ConstructError Projective
-> (Projective -> Either ConstructError Projective)
-> Maybe Projective
-> Either ConstructError Projective
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (ConstructError -> Either ConstructError Projective
forall a b. a -> Either a b
Left (Int -> ConstructError
InvalidHopPubKey Int
i)) Projective -> Either ConstructError Projective
forall a b. b -> Either a b
Right (Point -> Maybe Projective
from_point (Hop -> Point
hop_pubkey Hop
h))
encode_pl :: (Int, Hop) -> Either ConstructError ByteString
encode_pl (Int
i, Hop
h) = case HopPayload -> Either EncodeError ByteString
encode_hop_payload (Hop -> HopPayload
hop_payload Hop
h) of
Right ByteString
p | ByteString -> Int
BS.length ByteString
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
2 -> ByteString -> Either ConstructError ByteString
forall a b. b -> Either a b
Right ByteString
p
Either EncodeError ByteString
_ -> ConstructError -> Either ConstructError ByteString
forall a b. a -> Either a b
Left (Int -> ConstructError
InvalidHopPayload Int
i)
shift_size :: BS.ByteString -> Int
shift_size :: ByteString -> Int
shift_size ByteString
p =
let !l :: Int
l = ByteString -> Int
BS.length ByteString
p
in ByteString -> Int
BS.length (Word64 -> ByteString
BOLT1.encode_bigsize (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
l)) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
32
shared_secrets
:: BS.ByteString
-> Secp256k1.Projective
-> [(Int, Secp256k1.Projective)]
-> Either ConstructError [SharedSecret]
shared_secrets :: ByteString
-> Projective
-> [(Int, Projective)]
-> Either ConstructError [SharedSecret]
shared_secrets ByteString
_ Projective
_ [] = [SharedSecret] -> Either ConstructError [SharedSecret]
forall a b. b -> Either a b
Right []
shared_secrets ByteString
e Projective
epub ((Int
i, Projective
pub) : [(Int, Projective)]
rest) = do
ss <- Maybe SharedSecret -> Either ConstructError SharedSecret
forall {b}. Maybe b -> Either ConstructError b
note (ByteString -> Projective -> Maybe SharedSecret
ecdh ByteString
e Projective
pub)
case rest of
[] -> [SharedSecret] -> Either ConstructError [SharedSecret]
forall a b. b -> Either a b
Right [SharedSecret
ss]
[(Int, Projective)]
_ -> do
let bf :: ByteString
bf = Projective -> SharedSecret -> ByteString
blinding_factor Projective
epub SharedSecret
ss
e' <- Maybe ByteString -> Either ConstructError ByteString
forall {b}. Maybe b -> Either ConstructError b
note (ByteString -> ByteString -> Maybe ByteString
blind_scalar ByteString
e ByteString
bf)
epub' <- note (blind_pub epub bf)
(ss :) <$> shared_secrets e' epub' rest
where
note :: Maybe b -> Either ConstructError b
note = Either ConstructError b
-> (b -> Either ConstructError b)
-> Maybe b
-> Either ConstructError b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (ConstructError -> Either ConstructError b
forall a b. a -> Either a b
Left (Int -> ConstructError
InvalidHopPubKey Int
i)) b -> Either ConstructError b
forall a b. b -> Either a b
Right
filler :: [BS.ByteString] -> [Int] -> BS.ByteString
filler :: [ByteString] -> [Int] -> ByteString
filler [ByteString]
streams [Int]
sizes = (ByteString -> (ByteString, Int) -> ByteString)
-> ByteString -> [(ByteString, Int)] -> ByteString
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
L.foldl' ByteString -> (ByteString, Int) -> ByteString
step ByteString
BS.empty ([ByteString] -> [Int] -> [(ByteString, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [ByteString]
streams [Int]
sizes)
where
step :: ByteString -> (ByteString, Int) -> ByteString
step ByteString
f (ByteString
s, Int
n) =
let !ext :: ByteString
ext = ByteString
f ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Int -> Word8 -> ByteString
BS.replicate Int
n Word8
0
!off :: Int
off = Int
1300 Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
f
in ByteString -> ByteString -> ByteString
xor_bytes ByteString
ext (Int -> ByteString -> ByteString
BS.take (ByteString -> Int
BS.length ByteString
ext) (Int -> ByteString -> ByteString
BS.drop Int
off ByteString
s))
wrap
:: BS.ByteString
-> BS.ByteString
-> BS.ByteString
-> [(SharedSecret, BS.ByteString, BS.ByteString)]
-> (BS.ByteString, BS.ByteString)
wrap :: ByteString
-> ByteString
-> ByteString
-> [(SharedSecret, ByteString, ByteString)]
-> (ByteString, ByteString)
wrap ByteString
ad ByteString
fill = Bool
-> ByteString
-> ByteString
-> [(SharedSecret, ByteString, ByteString)]
-> (ByteString, ByteString)
go Bool
True (Int -> Word8 -> ByteString
BS.replicate Int
32 Word8
0)
where
go :: Bool
-> ByteString
-> ByteString
-> [(SharedSecret, ByteString, ByteString)]
-> (ByteString, ByteString)
go Bool
_ !ByteString
mac !ByteString
buf [] = (ByteString
buf, ByteString
mac)
go Bool
final !ByteString
mac !ByteString
buf ((SharedSecret
ss, ByteString
stream, ByteString
pl) : [(SharedSecret, ByteString, ByteString)]
rest) =
let !len :: ByteString
len = Word64 -> ByteString
BOLT1.encode_bigsize (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
BS.length ByteString
pl))
!n :: Int
n = ByteString -> Int
BS.length ByteString
len Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ByteString -> Int
BS.length ByteString
pl Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
32
!shifted :: ByteString
shifted = [ByteString] -> ByteString
BS.concat [ByteString
len, ByteString
pl, ByteString
mac, Int -> ByteString -> ByteString
BS.take (Int
1300 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) ByteString
buf]
!obf :: ByteString
obf = ByteString -> ByteString -> ByteString
xor_bytes ByteString
shifted ByteString
stream
!buf' :: ByteString
buf' | Bool
final = Int -> ByteString -> ByteString
BS.take (Int
1300 Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
fill) ByteString
obf ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
fill
| Bool
otherwise = ByteString
obf
!mac' :: ByteString
mac' = DerivedKey -> ByteString -> ByteString
hmac (SharedSecret -> DerivedKey
derive_mu SharedSecret
ss) (ByteString
buf' ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
ad)
in Bool
-> ByteString
-> ByteString
-> [(SharedSecret, ByteString, ByteString)]
-> (ByteString, ByteString)
go Bool
False ByteString
mac' ByteString
buf' [(SharedSecret, ByteString, ByteString)]
rest