diff options
Diffstat (limited to 'src/Crypto/Tox.hs')
-rw-r--r-- | src/Crypto/Tox.hs | 21 |
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 | |||
300 | lookupSharedSecret crypto sk recipient nonce | 302 | lookupSharedSecret crypto sk recipient nonce |
301 | = ($ nonce) <$> lookupNonceFunction crypto sk recipient | 303 | = ($ nonce) <$> lookupNonceFunction crypto sk recipient |
302 | 304 | ||
305 | {-# INLINE lookupNonceFunction #-} | ||
303 | lookupNonceFunction :: TransportCrypto -> SecretKey -> PublicKey -> IO (Nonce24 -> State) | 306 | lookupNonceFunction :: TransportCrypto -> SecretKey -> PublicKey -> IO (Nonce24 -> State) |
304 | lookupNonceFunction TransportCrypto{secretsCache} sk recipient = do | 307 | lookupNonceFunction 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. | ||
316 | lookupNonceFunctionSTM :: POSIXTime -> TransportCrypto -> SecretKey -> PublicKey -> STM (Nonce24 -> State) | ||
317 | lookupNonceFunctionSTM 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 | ||
334 | newtype Nonce24 = Nonce24 ByteString | 345 | newtype Nonce24 = Nonce24 ByteString |
335 | deriving (Eq, Ord, ByteArrayAccess,Data) | 346 | deriving (Eq, Ord, ByteArrayAccess, Data) |
347 | |||
348 | nonce24ToWord16 :: Nonce24 -> Word16 | ||
349 | nonce24ToWord16 (Nonce24 n24) = fromIntegral (B.index n24 23) + 256 * fromIntegral (B.index n24 22) | ||
336 | 350 | ||
337 | addtoNonce24 :: Nonce24 -> Word -> IO Nonce24 | 351 | addtoNonce24 :: Nonce24 -> Word -> IO Nonce24 |
338 | addtoNonce24 (Nonce24 n24) n = Nonce24 <$> BA.copy n24 init | 352 | addtoNonce24 (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 #-} | ||
379 | incrementNonce24 :: Nonce24 -> IO Nonce24 | 394 | incrementNonce24 :: Nonce24 -> IO Nonce24 |
380 | incrementNonce24 nonce24 = addtoNonce24 nonce24 1 | 395 | incrementNonce24 nonce24 = addtoNonce24 nonce24 1 |
381 | 396 | ||