{-# LANGUAGE ForeignFunctionInterface #-}-- |-- Module : Crypto.Cipher.Salsa-- License : BSD-style-- Maintainer : Vincent Hanquez <vincent@snarc.org>-- Stability : stable-- Portability : goodmodule Crypto.Cipher.Salsa ( initialize, combine, generate, State,) whereimport Crypto.Cipher.Salsa.Stateimport Crypto.Internal.ByteArray ( ByteArray, ByteArrayAccess, )import qualified Crypto.Internal.ByteArray as Bimport Crypto.Internal.Compatimport Crypto.Internal.Importsimport Foreign.C.Typesimport Foreign.Ptr-- | Initialize a new Salsa context with the number of rounds,-- the key and the nonce associated.initialize :: (ByteArrayAccess key, ByteArrayAccess nonce) => Int -- ^ number of rounds (8,12,20) -> key -- ^ the key (128 or 256 bits) -> nonce -- ^ the nonce (64 bits) -> State -- ^ the initial Salsa stateinitialize nbRounds key nonce | kLen `notElem` [16, 32] = error "Salsa: key length should be 128 or 256 bits" -- a 96-bit nonce used to be taken, its last word starting the block -- counter, so that nonces one apart shared all but a block of keystream | nonceLen /= 8 = error "Salsa: nonce length should be 64 bits" | nbRounds `notElem` [8, 12, 20] = error "Salsa: rounds should be 8, 12 or 20" | otherwise = unsafeDoIO $ do stPtr <- B.alloc 132 $ \stPtr -> B.withByteArray nonce $ \noncePtr -> B.withByteArray key $ \keyPtr -> ccrypton_salsa_init stPtr nbRounds kLen keyPtr nonceLen noncePtr return $ State stPtr where kLen = B.length key nonceLen = B.length nonce-- | Combine the salsa output and an arbitrary message with a xor,-- and return the combined output and the new state.combine :: ByteArray ba => State -- ^ the current Salsa state -> ba -- ^ the source to xor with the generator -> (ba, State)combine prevSt@(State prevStMem) src | B.null src = (B.empty, prevSt) | otherwise = unsafeDoIO $ do (out, st) <- B.copyRet prevStMem $ \ctx -> B.alloc n $ \dstPtr -> B.withByteArray src $ \srcPtr -> -- in pieces the C's uint32_t length can hold; it carries -- the state in ctx, so it can simply be called again B.inCLengths n $ \off len -> ccrypton_salsa_combine (dstPtr `plusPtr` off) ctx (srcPtr `plusPtr` off) (fromIntegral len) return (out, State st) where n = B.length src-- | Generate a number of bytes from the Salsa output directlygenerate :: ByteArray ba => State -- ^ the current Salsa state -> Int -- ^ the length of data to generate -> (ba, State)generate prevSt@(State prevStMem) len | len <= 0 = (B.empty, prevSt) | otherwise = unsafeDoIO $ do (out, st) <- B.copyRet prevStMem $ \ctx -> B.alloc len $ \dstPtr -> B.inCLengths len $ \off n -> ccrypton_salsa_generate (dstPtr `plusPtr` off) ctx (fromIntegral n) return (out, State st)foreign import ccall "crypton_salsa_init" ccrypton_salsa_init :: Ptr State -> Int -> Int -> Ptr Word8 -> Int -> Ptr Word8 -> IO ()foreign import ccall "crypton_salsa_combine" ccrypton_salsa_combine :: Ptr Word8 -> Ptr State -> Ptr Word8 -> CUInt -> IO ()foreign import ccall "crypton_salsa_generate" ccrypton_salsa_generate :: Ptr Word8 -> Ptr State -> CUInt -> IO ()