summaryrefslogtreecommitdiff
path: root/tox-crypto/src/Crypto/Tox.hs
diff options
context:
space:
mode:
Diffstat (limited to 'tox-crypto/src/Crypto/Tox.hs')
-rw-r--r--tox-crypto/src/Crypto/Tox.hs21
1 files changed, 14 insertions, 7 deletions
diff --git a/tox-crypto/src/Crypto/Tox.hs b/tox-crypto/src/Crypto/Tox.hs
index 602ead0a..aea684ec 100644
--- a/tox-crypto/src/Crypto/Tox.hs
+++ b/tox-crypto/src/Crypto/Tox.hs
@@ -41,6 +41,7 @@ module Crypto.Tox
41 , decodePlain 41 , decodePlain
42 -- , computeSharedSecret 42 -- , computeSharedSecret
43 , lookupSharedSecret 43 , lookupSharedSecret
44 , lookupSharedSecretSTM
44 , lookupNonceFunction 45 , lookupNonceFunction
45 , lookupNonceFunctionSTM 46 , lookupNonceFunctionSTM
46 , Payload(..) 47 , Payload(..)
@@ -369,11 +370,14 @@ lookupSharedSecret :: TransportCrypto -> SecretKey -> PublicKey -> Nonce24 -> IO
369lookupSharedSecret crypto sk recipient nonce 370lookupSharedSecret crypto sk recipient nonce
370 = ($ nonce) <$> lookupNonceFunction crypto sk recipient 371 = ($ nonce) <$> lookupNonceFunction crypto sk recipient
371 372
373lookupSharedSecretSTM :: TransportCrypto -> SecretKey -> PublicKey -> Nonce24 -> STM State
374lookupSharedSecretSTM crypto sk recipient nonce
375 = ($ nonce) <$> lookupNonceFunctionSTM crypto sk recipient
376
372{-# INLINE lookupNonceFunction #-} 377{-# INLINE lookupNonceFunction #-}
373lookupNonceFunction :: TransportCrypto -> SecretKey -> PublicKey -> IO (Nonce24 -> State) 378lookupNonceFunction :: TransportCrypto -> SecretKey -> PublicKey -> IO (Nonce24 -> State)
374lookupNonceFunction c@(TransportCrypto{secretsCache}) sk recipient = do 379lookupNonceFunction c@(TransportCrypto{secretsCache}) sk recipient = do
375 now <- getPOSIXTime 380 atomically $ lookupNonceFunctionSTM c sk recipient
376 atomically $ lookupNonceFunctionSTM now c sk recipient
377 381
378-- | Dummy, used to indicate computeSharedSecret failure. This is a way to provide 382-- | Dummy, used to indicate computeSharedSecret failure. This is a way to provide
379-- the old non-failing interface compatibly. 383-- the old non-failing interface compatibly.
@@ -389,8 +393,10 @@ dummyState = State hash xsalsa
389-- of the transaction fails, we may end up forgoing a computation that could have been cached. 393-- of the transaction fails, we may end up forgoing a computation that could have been cached.
390-- Use with care. In most circumstances you probably want 'lookupNonceFunction'. It also commits 394-- Use with care. In most circumstances you probably want 'lookupNonceFunction'. It also commits
391-- us to using TVars to store the cache. 395-- us to using TVars to store the cache.
392lookupNonceFunctionSTM :: POSIXTime -> TransportCrypto -> SecretKey -> PublicKey -> STM (Nonce24 -> State) 396lookupNonceFunctionSTM :: TransportCrypto -> SecretKey -> PublicKey -> STM (Nonce24 -> State)
393lookupNonceFunctionSTM now TransportCrypto{secretsCache} sk recipient = do 397lookupNonceFunctionSTM TransportCrypto{secretsCache} sk recipient = do
398 now <- readTVar $ secretsTime secretsCache
399 writeTVar (secretsTime secretsCache) $! now + 1
394 mm <- readTVar $ sharedSecret secretsCache 400 mm <- readTVar $ sharedSecret secretsCache
395 case MM.lookup' recipient mm of 401 case MM.lookup' recipient mm of
396 Nothing -> case computeSharedSecret sk recipient of 402 Nothing -> case computeSharedSecret sk recipient of
@@ -579,12 +585,13 @@ putAliasedAsymm (Asymm key nonce dta) = put nonce >> putPublicKey key >> put dta
579 585
580data SecretsCache = SecretsCache 586data SecretsCache = SecretsCache
581 { sharedSecret :: TVar (MinMaxPSQ' PublicKey 587 { sharedSecret :: TVar (MinMaxPSQ' PublicKey
582 (Down POSIXTime) 588 (Down Word64)
583 (MinMaxPSQ' SecretKey (Down POSIXTime) (Nonce24 -> State))) 589 (MinMaxPSQ' SecretKey (Down Word64) (Nonce24 -> State)))
590 , secretsTime :: TVar Word64
584 } 591 }
585 592
586newSecretsCache :: IO SecretsCache 593newSecretsCache :: IO SecretsCache
587newSecretsCache = atomically (SecretsCache <$> newTVar MM.empty) 594newSecretsCache = atomically (SecretsCache <$> newTVar MM.empty <*> newTVar 0)
588 595
589 596
590newtype SymmetricKey = SymmetricKey ByteString 597newtype SymmetricKey = SymmetricKey ByteString