diff options
Diffstat (limited to 'src/Network/Tox/DHT/Transport.hs')
-rw-r--r-- | src/Network/Tox/DHT/Transport.hs | 37 |
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 | ||
429 | encrypt :: TransportCrypto -> DHTMessage ((,) Nonce8) -> NodeInfo -> (DHTMessage Encrypted8, NodeInfo) | 429 | encrypt :: TransportCrypto -> DHTMessage ((,) Nonce8) -> NodeInfo -> IO (DHTMessage Encrypted8, NodeInfo) |
430 | encrypt crypto msg ni = ( transcode (encryptMessage crypto (id2key $ nodeId ni)) msg | 430 | encrypt 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 | ||
433 | encryptMessage :: Serialize a => | 435 | encryptMessage :: 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) |
437 | encryptMessage crypto destKey n arg = E8 $ ToxCrypto.encrypt secret plain | 439 | encryptMessage 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 | ||
442 | decrypt :: TransportCrypto -> DHTMessage Encrypted8 -> NodeInfo -> Either String (DHTMessage ((,) Nonce8), NodeInfo) | 444 | decrypt :: TransportCrypto -> DHTMessage Encrypted8 -> NodeInfo -> IO (Either String (DHTMessage ((,) Nonce8), NodeInfo)) |
443 | decrypt crypto msg ni = do | 445 | decrypt 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 | ||
447 | decryptMessage :: Serialize x => | 450 | decryptMessage :: 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) |
452 | decryptMessage crypto n arg = plain8 $ ToxCrypto.decrypt secret e | 455 | decryptMessage 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 | ||
458 | sequenceMessage :: Applicative m => DHTMessage (m ∘ f) -> m (DHTMessage f) | 461 | sequenceMessage :: Applicative m => DHTMessage (m ∘ f) -> m (DHTMessage f) |
459 | sequenceMessage (DHTPing asym) = fmap DHTPing $ sequenceA $ fmap uncomposed asym | 462 | sequenceMessage (DHTPing asym) = fmap DHTPing $ sequenceA $ fmap uncomposed asym |