summaryrefslogtreecommitdiff
path: root/src/Crypto/Tox.hs
diff options
context:
space:
mode:
authorJames Crayne <jim.crayne@gmail.com>2017-11-19 22:52:07 +0000
committerJames Crayne <jim.crayne@gmail.com>2017-11-19 23:40:18 +0000
commit8d9abc1df036a8184bc2fd88ddf6f1d621e7e4c1 (patch)
tree920d4c424e1d1358df0f066e4b5e6a256cd96da2 /src/Crypto/Tox.hs
parent5c34b3bffc286b6cc5010a30c1016355c86359a5 (diff)
Outgoing queue and related
Diffstat (limited to 'src/Crypto/Tox.hs')
-rw-r--r--src/Crypto/Tox.hs21
1 files changed, 18 insertions, 3 deletions
diff --git a/src/Crypto/Tox.hs b/src/Crypto/Tox.hs
index 18cdb5d2..624da233 100644
--- a/src/Crypto/Tox.hs
+++ b/src/Crypto/Tox.hs
@@ -34,11 +34,13 @@ module Crypto.Tox
34 , decodePlain 34 , decodePlain
35 -- , computeSharedSecret 35 -- , computeSharedSecret
36 , lookupSharedSecret 36 , lookupSharedSecret
37 , lookupNonceFunction
37 , encrypt 38 , encrypt
38 , decrypt 39 , decrypt
39 , Nonce8(..) 40 , Nonce8(..)
40 , Nonce24(..) 41 , Nonce24(..)
41 , incrementNonce24 42 , incrementNonce24
43 , nonce24ToWord16
42 , addtoNonce24 44 , addtoNonce24
43 , Nonce32(..) 45 , Nonce32(..)
44 , getRemainingEncrypted 46 , getRemainingEncrypted
@@ -300,10 +302,19 @@ lookupSharedSecret :: TransportCrypto -> SecretKey -> PublicKey -> Nonce24 -> IO
300lookupSharedSecret crypto sk recipient nonce 302lookupSharedSecret crypto sk recipient nonce
301 = ($ nonce) <$> lookupNonceFunction crypto sk recipient 303 = ($ nonce) <$> lookupNonceFunction crypto sk recipient
302 304
305{-# INLINE lookupNonceFunction #-}
303lookupNonceFunction :: TransportCrypto -> SecretKey -> PublicKey -> IO (Nonce24 -> State) 306lookupNonceFunction :: TransportCrypto -> SecretKey -> PublicKey -> IO (Nonce24 -> State)
304lookupNonceFunction TransportCrypto{secretsCache} sk recipient = do 307lookupNonceFunction c@(TransportCrypto{secretsCache}) sk recipient = do
305 now <- getPOSIXTime 308 now <- getPOSIXTime
306 atomically $ do 309 atomically $ lookupNonceFunctionSTM now c sk recipient
310
311{-# INLINE lookupNonceFunctionSTM #-}
312-- | This version of 'lookupNonceFunction' is STM instead of IO, this means if some later part of
313-- of the transaction fails, we may end up forgoing a computation that could have been cached.
314-- Use with care. In most circumstances you probably want 'lookupNonceFunction'. It also commits
315-- us to using TVars to store the cache.
316lookupNonceFunctionSTM :: POSIXTime -> TransportCrypto -> SecretKey -> PublicKey -> STM (Nonce24 -> State)
317lookupNonceFunctionSTM now TransportCrypto{secretsCache} sk recipient = do
307 mm <- readTVar $ sharedSecret secretsCache 318 mm <- readTVar $ sharedSecret secretsCache
308 case MM.lookup' recipient mm of 319 case MM.lookup' recipient mm of
309 Nothing -> do 320 Nothing -> do
@@ -332,7 +343,10 @@ hsalsa20 k n = BA.append a b
332 343
333 344
334newtype Nonce24 = Nonce24 ByteString 345newtype Nonce24 = Nonce24 ByteString
335 deriving (Eq, Ord, ByteArrayAccess,Data) 346 deriving (Eq, Ord, ByteArrayAccess, Data)
347
348nonce24ToWord16 :: Nonce24 -> Word16
349nonce24ToWord16 (Nonce24 n24) = fromIntegral (B.index n24 23) + 256 * fromIntegral (B.index n24 22)
336 350
337addtoNonce24 :: Nonce24 -> Word -> IO Nonce24 351addtoNonce24 :: Nonce24 -> Word -> IO Nonce24
338addtoNonce24 (Nonce24 n24) n = Nonce24 <$> BA.copy n24 init 352addtoNonce24 (Nonce24 n24) n = Nonce24 <$> BA.copy n24 init
@@ -376,6 +390,7 @@ addtoNonce24 (Nonce24 n24) n = Nonce24 <$> BA.copy n24 init
376 pokeElemOff ptr 5 $ tBE32 (W# sum_) 390 pokeElemOff ptr 5 $ tBE32 (W# sum_)
377 init _ = error "incrementNonce24: I only support 64 and 32 bits" 391 init _ = error "incrementNonce24: I only support 64 and 32 bits"
378 392
393{-# INLINE incrementNonce24 #-}
379incrementNonce24 :: Nonce24 -> IO Nonce24 394incrementNonce24 :: Nonce24 -> IO Nonce24
380incrementNonce24 nonce24 = addtoNonce24 nonce24 1 395incrementNonce24 nonce24 = addtoNonce24 nonce24 1
381 396