diff options
author | James Crayne <jim.crayne@gmail.com> | 2019-09-28 13:43:29 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-01 19:27:53 -0500 |
commit | 11987749fc6e6d3e53ea737d46d5ab13a16faeb8 (patch) | |
tree | 5716463275c2d3e902889db619908ded2a73971c /cryptonite-backport/Crypto/Cipher/Salsa.hs | |
parent | add2c76bced51fde5e9917e7449ef52be70faf87 (diff) |
Factor out some new libraries
word64-map:
Data.Word64Map
network-addr:
Network.Address
tox-crypto:
Crypto.Tox
lifted-concurrent:
Control.Concurrent.Lifted.Instrument
Control.Concurrent.Async.Lifted.Instrument
psq-wrap:
Data.Wrapper.PSQInt
Data.Wrapper.PSQ
minmax-psq:
Data.MinMaxPSQ
tasks:
Control.Concurrent.Tasks
kad:
Network.Kademlia
Network.Kademlia.Bootstrap
Network.Kademlia.Routing
Network.Kademlia.CommonAPI
Network.Kademlia.Persistence
Network.Kademlia.Search
Diffstat (limited to 'cryptonite-backport/Crypto/Cipher/Salsa.hs')
-rw-r--r-- | cryptonite-backport/Crypto/Cipher/Salsa.hs | 83 |
1 files changed, 0 insertions, 83 deletions
diff --git a/cryptonite-backport/Crypto/Cipher/Salsa.hs b/cryptonite-backport/Crypto/Cipher/Salsa.hs deleted file mode 100644 index b6b188b1..00000000 --- a/cryptonite-backport/Crypto/Cipher/Salsa.hs +++ /dev/null | |||
@@ -1,83 +0,0 @@ | |||
1 | -- | | ||
2 | -- Module : Crypto.Cipher.Salsa | ||
3 | -- License : BSD-style | ||
4 | -- Maintainer : Vincent Hanquez <vincent@snarc.org> | ||
5 | -- Stability : stable | ||
6 | -- Portability : good | ||
7 | -- | ||
8 | {-# LANGUAGE ForeignFunctionInterface #-} | ||
9 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
10 | module Crypto.Cipher.Salsa | ||
11 | ( initialize | ||
12 | , combine | ||
13 | , generate | ||
14 | , State(..) | ||
15 | ) where | ||
16 | |||
17 | import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray, ScrubbedBytes) | ||
18 | import qualified Crypto.Internal.ByteArray as B | ||
19 | import Crypto.Internal.Compat | ||
20 | import Crypto.Internal.Imports | ||
21 | import Foreign.Ptr | ||
22 | import Foreign.C.Types | ||
23 | |||
24 | -- | Salsa context | ||
25 | newtype State = State ScrubbedBytes | ||
26 | deriving (NFData) | ||
27 | |||
28 | -- | Initialize a new Salsa context with the number of rounds, | ||
29 | -- the key and the nonce associated. | ||
30 | initialize :: (ByteArrayAccess key, ByteArrayAccess nonce) | ||
31 | => Int -- ^ number of rounds (8,12,20) | ||
32 | -> key -- ^ the key (128 or 256 bits) | ||
33 | -> nonce -- ^ the nonce (64 or 96 bits) | ||
34 | -> State -- ^ the initial Salsa state | ||
35 | initialize nbRounds key nonce | ||
36 | | not (kLen `elem` [16,32]) = error "Salsa: key length should be 128 or 256 bits" | ||
37 | | not (nonceLen `elem` [8,12]) = error "Salsa: nonce length should be 64 or 96 bits" | ||
38 | | not (nbRounds `elem` [8,12,20]) = error "Salsa: rounds should be 8, 12 or 20" | ||
39 | | otherwise = unsafeDoIO $ do | ||
40 | stPtr <- B.alloc 132 $ \stPtr -> | ||
41 | B.withByteArray nonce $ \noncePtr -> | ||
42 | B.withByteArray key $ \keyPtr -> | ||
43 | ccryptonite_salsa_init stPtr (fromIntegral nbRounds) kLen keyPtr nonceLen noncePtr | ||
44 | return $ State stPtr | ||
45 | where kLen = B.length key | ||
46 | nonceLen = B.length nonce | ||
47 | |||
48 | -- | Combine the salsa output and an arbitrary message with a xor, | ||
49 | -- and return the combined output and the new state. | ||
50 | combine :: ByteArray ba | ||
51 | => State -- ^ the current Salsa state | ||
52 | -> ba -- ^ the source to xor with the generator | ||
53 | -> (ba, State) | ||
54 | combine prevSt@(State prevStMem) src | ||
55 | | B.null src = (B.empty, prevSt) | ||
56 | | otherwise = unsafeDoIO $ do | ||
57 | (out, st) <- B.copyRet prevStMem $ \ctx -> | ||
58 | B.alloc (B.length src) $ \dstPtr -> | ||
59 | B.withByteArray src $ \srcPtr -> do | ||
60 | ccryptonite_salsa_combine dstPtr ctx srcPtr (fromIntegral $ B.length src) | ||
61 | return (out, State st) | ||
62 | |||
63 | -- | Generate a number of bytes from the Salsa output directly | ||
64 | generate :: ByteArray ba | ||
65 | => State -- ^ the current Salsa state | ||
66 | -> Int -- ^ the length of data to generate | ||
67 | -> (ba, State) | ||
68 | generate prevSt@(State prevStMem) len | ||
69 | | len <= 0 = (B.empty, prevSt) | ||
70 | | otherwise = unsafeDoIO $ do | ||
71 | (out, st) <- B.copyRet prevStMem $ \ctx -> | ||
72 | B.alloc len $ \dstPtr -> | ||
73 | ccryptonite_salsa_generate dstPtr ctx (fromIntegral len) | ||
74 | return (out, State st) | ||
75 | |||
76 | foreign import ccall "cryptonite_salsa_init" | ||
77 | ccryptonite_salsa_init :: Ptr State -> Int -> Int -> Ptr Word8 -> Int -> Ptr Word8 -> IO () | ||
78 | |||
79 | foreign import ccall "cryptonite_salsa_combine" | ||
80 | ccryptonite_salsa_combine :: Ptr Word8 -> Ptr State -> Ptr Word8 -> CUInt -> IO () | ||
81 | |||
82 | foreign import ccall "cryptonite_salsa_generate" | ||
83 | ccryptonite_salsa_generate :: Ptr Word8 -> Ptr State -> CUInt -> IO () | ||