packages feed

crypton-2.3.1 / Crypto / Cipher / RC4.hs

{-# LANGUAGE ForeignFunctionInterface #-}{-# LANGUAGE GeneralizedNewtypeDeriving #-}-- |-- Module      : Crypto.Cipher.RC4-- License     : BSD-style-- Maintainer  : Vincent Hanquez <vincent@snarc.org>-- Stability   : stable-- Portability : Good---- Simple implementation of the RC4 stream cipher.-- http://en.wikipedia.org/wiki/RC4---- Initial FFI implementation by Peter White <peter@janrain.com>---- Reorganized and simplified to have an opaque context.module Crypto.Cipher.RC4 (    initialize,    combine,    generate,    State,) whereimport Crypto.Internal.ByteArray (    ByteArray,    ByteArrayAccess,    ScrubbedBytes, )import qualified Crypto.Internal.ByteArray as Bimport Data.Wordimport Foreign.Ptrimport Crypto.Internal.Compatimport Crypto.Internal.Imports-- | The encryption state for RC4---- This type is an instance of 'ByteArrayAccess' for debugging purpose. Internal-- layout is architecture dependent, may contain uninitialized data fragments,-- and change in future versions.  The bytearray should not be used as input to-- cryptographic algorithms.newtype State = State ScrubbedBytes    deriving (ByteArrayAccess, NFData)-- | C Call for initializing the encryptorforeign import ccall unsafe "crypton_rc4.h crypton_rc4_init"    c_rc4_init        :: Ptr Word8        -- ^ The rc4 key        -> Word32        -- ^ The key length        -> Ptr State        -- ^ The context        -> IO ()foreign import ccall unsafe "crypton_rc4.h crypton_rc4_combine"    c_rc4_combine        :: Ptr State        -- ^ Pointer to the permutation        -> Ptr Word8        -- ^ Pointer to the clear text        -> Word32        -- ^ Length of the clear text        -> Ptr Word8        -- ^ Output buffer        -> IO ()-- | RC4 context initialization.---- seed the context with an initial key. the key size need to be-- adequate otherwise security takes a hit.initialize    :: ByteArrayAccess key    => key    -- ^ The key    -> State    -- ^ The RC4 context with the key mixed ininitialize key    | B.length key < 1 = error "Crypto.Cipher.RC4: the key must not be empty"    | otherwise = unsafeDoIO $ do        -- only the first 256 bytes are used, and the C takes a uint32_t        st <- B.alloc 264 $ \stPtr ->            B.withByteArray key $ \keyPtr ->                c_rc4_init keyPtr (fromIntegral $ min 256 $ B.length key) (castPtr stPtr)        return $ State st-- | generate the next len bytes of the rc4 stream without combining-- it to anything.generate :: ByteArray ba => State -> Int -> (State, ba)generate ctx len = combine ctx (B.zero len)-- | RC4 xor combination of the rc4 stream with an inputcombine    :: ByteArray ba    => State    -- ^ rc4 context    -> ba    -- ^ input    -> (State, ba)    -- ^ new rc4 context, and the outputcombine (State prevSt) clearText = unsafeDoIO $    B.allocRet len $ \outptr ->        B.withByteArray clearText $ \clearPtr -> do            st <- B.copy prevSt $ \stPtr ->                -- in pieces the C's uint32_t length can hold; the state it                -- keeps means it can simply be called again                B.inCLengths len $ \off n ->                    c_rc4_combine                        (castPtr stPtr)                        (clearPtr `plusPtr` off)                        (fromIntegral n)                        (outptr `plusPtr` off)            return $! State st  where    -- return $! (State st, B.PS outfptr 0 len)    len = B.length clearText