Fixture.hs (2644B)
1 module Fixture ( 2 i_s_ent 3 , i_e_ent 4 , r_s_ent 5 , r_e_ent 6 , msg_32 7 , msg_1k 8 , msg_64k 9 , Fixture(..) 10 , fixture 11 ) where 12 13 import qualified Data.ByteString as BS 14 import qualified Lightning.Protocol.BOLT8 as BOLT8 15 16 -- key material from BOLT #8 Appendix A 17 i_s_ent, i_e_ent, r_s_ent, r_e_ent :: BS.ByteString 18 i_s_ent = BS.replicate 32 0x11 19 i_e_ent = BS.replicate 32 0x12 20 r_s_ent = BS.replicate 32 0x21 21 r_e_ent = BS.replicate 32 0x22 22 23 data Fixture = Fixture { 24 fx_i :: !BOLT8.Keypair 25 , fx_r :: !BOLT8.Keypair 26 , fx_msg1 :: !BS.ByteString 27 , fx_i_hs :: !BOLT8.Initiator 28 , fx_msg2 :: !BS.ByteString 29 , fx_r_hs :: !BOLT8.Responder 30 , fx_msg3 :: !BS.ByteString 31 , fx_snd :: !BOLT8.Sender 32 , fx_snd_rot :: !BOLT8.Sender -- its next encrypt rotates the key 33 , fx_rcv :: !BOLT8.Receiver 34 , fx_ct_32 :: !BS.ByteString 35 , fx_ct_1k :: !BS.ByteString 36 , fx_ct_64k :: !BS.ByteString 37 , fx_pend_1k :: !BOLT8.Pending -- awaiting the body of fx_ct_1k 38 } 39 40 fixture :: IO Fixture 41 fixture = do 42 i <- orFail "keypair" (maybe (Left ()) Right (BOLT8.keypair i_s_ent)) 43 r <- orFail "keypair" (maybe (Left ()) Right (BOLT8.keypair r_s_ent)) 44 (msg1, i_hs) <- orFail "act1" (BOLT8.act1 i (BOLT8.keypair_pub r) i_e_ent) 45 (msg2, r_hs) <- orFail "act2" (BOLT8.act2 r r_e_ent msg1) 46 (msg3, i_res) <- orFail "act3" (BOLT8.act3 i_hs msg2) 47 r_res <- orFail "finalize" (BOLT8.finalize r_hs msg3) 48 let snd0 = BOLT8.handshake_sender i_res 49 rcv0 = BOLT8.handshake_receiver r_res 50 snd_rot <- advance (499 :: Int) snd0 51 (ct_32, _) <- orFail "encrypt" (BOLT8.encrypt snd0 msg_32) 52 (ct_1k, _) <- orFail "encrypt" (BOLT8.encrypt snd0 msg_1k) 53 (ct_64k, _) <- orFail "encrypt" (BOLT8.encrypt snd0 msg_64k) 54 (_, pend_1k) <- orFail "header" 55 (BOLT8.decrypt_header rcv0 (BS.take 18 ct_1k)) 56 -- benchmark the success path, not MAC failure 57 mapM_ (orFail "decrypt" . BOLT8.decrypt rcv0) [ct_32, ct_1k, ct_64k] 58 pure Fixture { 59 fx_i = i, fx_r = r 60 , fx_msg1 = msg1, fx_i_hs = i_hs 61 , fx_msg2 = msg2, fx_r_hs = r_hs 62 , fx_msg3 = msg3 63 , fx_snd = snd0, fx_snd_rot = snd_rot 64 , fx_rcv = rcv0 65 , fx_ct_32 = ct_32, fx_ct_1k = ct_1k, fx_ct_64k = ct_64k 66 , fx_pend_1k = pend_1k 67 } 68 where 69 orFail :: Show e => String -> Either e a -> IO a 70 orFail msg = either (\e -> fail (msg <> ": " <> show e)) pure 71 advance 0 s = pure s 72 advance n s = do 73 (_, s') <- orFail "encrypt" (BOLT8.encrypt s msg_32) 74 advance (n - 1) s' 75 76 msg_32, msg_1k, msg_64k :: BS.ByteString 77 msg_32 = BS.replicate 32 0x00 78 msg_1k = BS.replicate 1024 0x00 79 msg_64k = BS.replicate 65535 0x00