summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2018-09-04 23:08:58 -0400
committerJoe Crayne <joe@jerkface.net>2018-09-07 13:18:56 -0400
commit18eb72eca608b5858be66a4d48c6818556b8e124 (patch)
tree4cecb20dfce635135ef92da23550ecdb9dfaa8ca
parentd133be0736d6ce366a41582bc59501e6eab81163 (diff)
tox: Generate keys within STM monad.
-rw-r--r--src/Crypto/Tox.hs7
-rw-r--r--src/Network/Tox.hs5
2 files changed, 10 insertions, 2 deletions
diff --git a/src/Crypto/Tox.hs b/src/Crypto/Tox.hs
index a1f40c76..c44282cf 100644
--- a/src/Crypto/Tox.hs
+++ b/src/Crypto/Tox.hs
@@ -4,6 +4,7 @@
4{-# LANGUAGE KindSignatures #-} 4{-# LANGUAGE KindSignatures #-}
5{-# LANGUAGE DeriveDataTypeable #-} 5{-# LANGUAGE DeriveDataTypeable #-}
6{-# LANGUAGE DeriveFunctor #-} 6{-# LANGUAGE DeriveFunctor #-}
7{-# LANGUAGE DeriveGeneric #-}
7{-# LANGUAGE DeriveTraversable #-} 8{-# LANGUAGE DeriveTraversable #-}
8{-# LANGUAGE ExplicitNamespaces #-} 9{-# LANGUAGE ExplicitNamespaces #-}
9{-# LANGUAGE TypeOperators #-} 10{-# LANGUAGE TypeOperators #-}
@@ -99,6 +100,7 @@ import Crypto.Error
99#endif 100#endif
100import Network.Socket (SockAddr) 101import Network.Socket (SockAddr)
101import GHC.Exts (Word(..),inline) 102import GHC.Exts (Word(..),inline)
103import GHC.Generics (Generic)
102import GHC.Prim 104import GHC.Prim
103import Data.Word64Map (fitsInInt) 105import Data.Word64Map (fitsInInt)
104import Data.MinMaxPSQ (MinMaxPSQ') 106import Data.MinMaxPSQ (MinMaxPSQ')
@@ -109,7 +111,7 @@ import System.IO.Unsafe (unsafeDupablePerformIO)
109 111
110-- | A 16-byte mac and an arbitrary-length encrypted stream. 112-- | A 16-byte mac and an arbitrary-length encrypted stream.
111newtype Encrypted a = Encrypted ByteString 113newtype Encrypted a = Encrypted ByteString
112 deriving (Eq,Ord,Data,ByteArrayAccess) 114 deriving (Eq,Ord,Data,ByteArrayAccess,Hashable,Generic)
113 115
114newtype Encrypted8 a = E8 (Encrypted (a,Nonce8)) 116newtype Encrypted8 a = E8 (Encrypted (a,Nonce8))
115 deriving (Serialize, Show) 117 deriving (Serialize, Show)
@@ -358,7 +360,7 @@ hsalsa20 k n = BA.append a b
358 360
359 361
360newtype Nonce24 = Nonce24 ByteString 362newtype Nonce24 = Nonce24 ByteString
361 deriving (Eq, Ord, ByteArrayAccess, Data) 363 deriving (Eq, Ord, ByteArrayAccess, Data, Generic, Hashable)
362 364
363nonce24ToWord16 :: Nonce24 -> Word16 365nonce24ToWord16 :: Nonce24 -> Word16
364nonce24ToWord16 (Nonce24 n24) = fromIntegral (B.index n24 23) + 256 * fromIntegral (B.index n24 22) 366nonce24ToWord16 (Nonce24 n24) = fromIntegral (B.index n24 23) + 256 * fromIntegral (B.index n24 22)
@@ -536,6 +538,7 @@ data TransportCrypto = TransportCrypto
536 , rendezvousPublic :: PublicKey 538 , rendezvousPublic :: PublicKey
537 , transportSymmetric :: STM SymmetricKey 539 , transportSymmetric :: STM SymmetricKey
538 , transportNewNonce :: STM Nonce24 540 , transportNewNonce :: STM Nonce24
541 , transportNewKey :: STM SecretKey
539 , userKeys :: STM [(SecretKey,PublicKey)] 542 , userKeys :: STM [(SecretKey,PublicKey)]
540 , pendingCookies :: TVar [(SockAddr, (Int, PublicKey))] 543 , pendingCookies :: TVar [(SockAddr, (Int, PublicKey))]
541 , secretsCache :: SecretsCache 544 , secretsCache :: SecretsCache
diff --git a/src/Network/Tox.hs b/src/Network/Tox.hs
index 1bf6efc5..7011618c 100644
--- a/src/Network/Tox.hs
+++ b/src/Network/Tox.hs
@@ -101,6 +101,11 @@ newCrypto = do
101 let (nonce, drg2) = withDRG drg1 (Nonce24 <$> getRandomBytes 24) 101 let (nonce, drg2) = withDRG drg1 (Nonce24 <$> getRandomBytes 24)
102 writeTVar noncevar drg2 102 writeTVar noncevar drg2
103 return nonce 103 return nonce
104 , transportNewKey = do
105 drg1 <- readTVar noncevar
106 let (k, drg2) = withDRG drg1 generateSecretKey
107 writeTVar noncevar drg2
108 return k
104 , userKeys = return [] 109 , userKeys = return []
105 , pendingCookies = cookieKeys 110 , pendingCookies = cookieKeys
106 , secretsCache = cache 111 , secretsCache = cache