summaryrefslogtreecommitdiff
path: root/src/Network/Tox/DHT/Transport.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/Tox/DHT/Transport.hs')
-rw-r--r--src/Network/Tox/DHT/Transport.hs37
1 files changed, 20 insertions, 17 deletions
diff --git a/src/Network/Tox/DHT/Transport.hs b/src/Network/Tox/DHT/Transport.hs
index 736e84d1..bd108276 100644
--- a/src/Network/Tox/DHT/Transport.hs
+++ b/src/Network/Tox/DHT/Transport.hs
@@ -426,34 +426,37 @@ forwardDHTRequests crypto closeLookup dht = dht { awaitMessage = await' }
426 await' pass 426 await' pass
427 m -> pass m 427 m -> pass m
428 428
429encrypt :: TransportCrypto -> DHTMessage ((,) Nonce8) -> NodeInfo -> (DHTMessage Encrypted8, NodeInfo) 429encrypt :: TransportCrypto -> DHTMessage ((,) Nonce8) -> NodeInfo -> IO (DHTMessage Encrypted8, NodeInfo)
430encrypt crypto msg ni = ( transcode (encryptMessage crypto (id2key $ nodeId ni)) msg 430encrypt crypto msg ni = do
431 , ni ) 431 let cipher n plain = Composed $ encryptMessage crypto (id2key $ nodeId ni) n plain
432 m <- sequenceMessage $ transcode cipher msg
433 return (m, ni)
432 434
433encryptMessage :: Serialize a => 435encryptMessage :: Serialize a =>
434 TransportCrypto -> 436 TransportCrypto ->
435 PublicKey -> 437 PublicKey ->
436 Nonce24 -> Either (Nonce8,a) (Asymm (Nonce8,a)) -> Encrypted8 a 438 Nonce24 -> Either (Nonce8,a) (Asymm (Nonce8,a)) -> IO (Encrypted8 a)
437encryptMessage crypto destKey n arg = E8 $ ToxCrypto.encrypt secret plain 439encryptMessage crypto destKey n arg = do
438 where 440 let plain = encodePlain $ swap $ either id asymmData arg
439 secret = computeSharedSecret (transportSecret crypto) destKey n 441 secret <- lookupSharedSecret crypto (transportSecret crypto) destKey n
440 plain = encodePlain $ swap $ either id asymmData arg 442 return $ E8 $ ToxCrypto.encrypt secret plain
441 443
442decrypt :: TransportCrypto -> DHTMessage Encrypted8 -> NodeInfo -> Either String (DHTMessage ((,) Nonce8), NodeInfo) 444decrypt :: TransportCrypto -> DHTMessage Encrypted8 -> NodeInfo -> IO (Either String (DHTMessage ((,) Nonce8), NodeInfo))
443decrypt crypto msg ni = do 445decrypt crypto msg ni = do
444 msg' <- sequenceMessage $ transcode (\n -> decryptMessage crypto n . left ((,) $ id2key $ nodeId ni)) msg 446 let decipher n c = Composed $ decryptMessage crypto n . left ((,) $ id2key $ nodeId ni) $ c
445 return (msg', ni) 447 msg' <- sequenceMessage $ transcode decipher msg
448 return $ fmap (, ni) $ sequenceMessage msg'
446 449
447decryptMessage :: Serialize x => 450decryptMessage :: Serialize x =>
448 TransportCrypto 451 TransportCrypto
449 -> Nonce24 452 -> Nonce24
450 -> Either (PublicKey, Encrypted8 x) (Asymm (Encrypted8 x)) 453 -> Either (PublicKey, Encrypted8 x) (Asymm (Encrypted8 x))
451 -> (Either String ∘ ((,) Nonce8)) x 454 -> IO ((Either String ∘ ((,) Nonce8)) x)
452decryptMessage crypto n arg = plain8 $ ToxCrypto.decrypt secret e 455decryptMessage crypto n arg = do
453 where 456 let (remotekey,E8 e) = either id (senderKey &&& asymmData) arg
454 secret = computeSharedSecret (transportSecret crypto) remotekey n 457 plain8 = Composed . fmap swap . (>>= decodePlain)
455 (remotekey,E8 e) = either id (senderKey &&& asymmData) arg 458 secret <- lookupSharedSecret crypto (transportSecret crypto) remotekey n
456 plain8 = Composed . fmap swap . (>>= decodePlain) 459 return $ plain8 $ ToxCrypto.decrypt secret e
457 460
458sequenceMessage :: Applicative m => DHTMessage (m ∘ f) -> m (DHTMessage f) 461sequenceMessage :: Applicative m => DHTMessage (m ∘ f) -> m (DHTMessage f)
459sequenceMessage (DHTPing asym) = fmap DHTPing $ sequenceA $ fmap uncomposed asym 462sequenceMessage (DHTPing asym) = fmap DHTPing $ sequenceA $ fmap uncomposed asym