{-# LANGUAGE CPP #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module OpenSSL.EVP.Open
( open
, openBS
, openLBS
)
where
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy.Char8 as L8
import qualified Data.ByteString.Unsafe as B8
import Foreign.C.String (CString)
#if MIN_VERSION_base(4,5,0)
import Foreign.C.Types (CChar(..), CInt(..))
#else
import Foreign.C.Types (CChar, CInt)
#endif
import Foreign.Ptr (Ptr)
import OpenSSL.EVP.Cipher hiding (cipher)
import OpenSSL.EVP.PKey
import OpenSSL.EVP.Internal
import OpenSSL.Utils
import System.IO.Unsafe (unsafePerformIO)
foreign import ccall unsafe "EVP_OpenInit"
_OpenInit :: Ptr EVP_CIPHER_CTX
-> Cipher
-> Ptr CChar
-> CInt
-> CString
-> Ptr EVP_PKEY
-> IO CInt
openInit :: KeyPair key =>
Cipher
-> B8.ByteString
-> B8.ByteString
-> key
-> IO CipherCtx
openInit cipher encKey iv pkey
= do ctx <- newCipherCtx
withCipherCtxPtr ctx $ \ ctxPtr ->
B8.unsafeUseAsCStringLen encKey $ \ (encKeyPtr, encKeyLen) ->
B8.unsafeUseAsCString iv $ \ ivPtr ->
withPKeyPtr' pkey $ \ pkeyPtr ->
_OpenInit ctxPtr cipher encKeyPtr (fromIntegral encKeyLen) ivPtr pkeyPtr
>>= failIf_ (== 0)
return ctx
open :: KeyPair key =>
Cipher
-> String
-> String
-> key
-> String
-> String
{-# DEPRECATED open "Use openBS or openLBS instead." #-}
open cipher encKey iv pkey input
= L8.unpack $ openLBS cipher (B8.pack encKey) (B8.pack iv) pkey (L8.pack input)
openBS :: KeyPair key =>
Cipher
-> B8.ByteString
-> B8.ByteString
-> key
-> B8.ByteString
-> B8.ByteString
openBS cipher encKey iv pkey input
= unsafePerformIO $
do ctx <- openInit cipher encKey iv pkey
cipherStrictly ctx input
openLBS :: KeyPair key =>
Cipher
-> B8.ByteString
-> B8.ByteString
-> key
-> L8.ByteString
-> L8.ByteString
openLBS cipher encKey iv pkey input
= unsafePerformIO $
do ctx <- openInit cipher encKey iv pkey
cipherLazily ctx input