{-# LANGUAGE CPP #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE CApiFFI #-}
module OpenSSL.EVP.Cipher
( Cipher
, getCipherByName
, getCipherNames
, CryptoMode(..)
, cipher
, cipherBS
, cipherLBS
, cipherStrictLBS
)
where
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy.Char8 as L8
import Foreign
import Foreign.C
import OpenSSL.Objects
import OpenSSL.EVP.Internal
#if !MIN_VERSION_base(4,8,0)
import Data.Monoid
#endif
foreign import capi unsafe "openssl/evp.h EVP_get_cipherbyname"
_get_cipherbyname :: CString -> IO (Ptr EVP_CIPHER)
getCipherByName :: String -> IO (Maybe Cipher)
getCipherByName :: String -> IO (Maybe Cipher)
getCipherByName String
name
= String -> (CString -> IO (Maybe Cipher)) -> IO (Maybe Cipher)
forall a. String -> (CString -> IO a) -> IO a
withCString String
name ((CString -> IO (Maybe Cipher)) -> IO (Maybe Cipher))
-> (CString -> IO (Maybe Cipher)) -> IO (Maybe Cipher)
forall a b. (a -> b) -> a -> b
$ \ CString
namePtr ->
do ptr <- CString -> IO (Ptr EVP_CIPHER)
_get_cipherbyname CString
namePtr
if ptr == nullPtr then
return Nothing
else
return $ Just $ Cipher ptr
getCipherNames :: IO [String]
getCipherNames :: IO [String]
getCipherNames = ObjNameType -> Bool -> IO [String]
getObjNames ObjNameType
CipherMethodType Bool
True
cipherStrictLBS :: Cipher
-> B8.ByteString
-> B8.ByteString
-> CryptoMode
-> L8.ByteString
-> IO L8.ByteString
cipherStrictLBS :: Cipher
-> StrictByteString
-> StrictByteString
-> CryptoMode
-> ByteString
-> IO ByteString
cipherStrictLBS Cipher
c StrictByteString
key StrictByteString
iv CryptoMode
mode ByteString
input =
do ctx <- Cipher
-> StrictByteString
-> StrictByteString
-> CryptoMode
-> IO CipherCtx
cipherInitBS Cipher
c StrictByteString
key StrictByteString
iv CryptoMode
mode
xs <- cipherUpdateBS ctx `mapM` L8.toChunks input
x <- cipherFinalBS ctx
return $ L8.fromChunks (xs `mappend` [x])
cipher :: Cipher
-> String
-> String
-> CryptoMode
-> String
-> IO String
{-# DEPRECATED cipher "Use cipherBS, cipherLBS or cipherStrictLBS." #-}
cipher :: Cipher -> String -> String -> CryptoMode -> String -> IO String
cipher Cipher
c String
key String
iv CryptoMode
mode String
input
= (ByteString -> String) -> IO ByteString -> IO String
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> String
L8.unpack (IO ByteString -> IO String) -> IO ByteString -> IO String
forall a b. (a -> b) -> a -> b
$ Cipher
-> StrictByteString
-> StrictByteString
-> CryptoMode
-> ByteString
-> IO ByteString
cipherLBS Cipher
c (String -> StrictByteString
B8.pack String
key) (String -> StrictByteString
B8.pack String
iv) CryptoMode
mode (String -> ByteString
L8.pack String
input)
cipherBS :: Cipher
-> B8.ByteString
-> B8.ByteString
-> CryptoMode
-> B8.ByteString
-> IO B8.ByteString
cipherBS :: Cipher
-> StrictByteString
-> StrictByteString
-> CryptoMode
-> StrictByteString
-> IO StrictByteString
cipherBS Cipher
c StrictByteString
key StrictByteString
iv CryptoMode
mode StrictByteString
input
= do ctx <- Cipher
-> StrictByteString
-> StrictByteString
-> CryptoMode
-> IO CipherCtx
cipherInitBS Cipher
c StrictByteString
key StrictByteString
iv CryptoMode
mode
cipherStrictly ctx input
cipherLBS :: Cipher
-> B8.ByteString
-> B8.ByteString
-> CryptoMode
-> L8.ByteString
-> IO L8.ByteString
cipherLBS :: Cipher
-> StrictByteString
-> StrictByteString
-> CryptoMode
-> ByteString
-> IO ByteString
cipherLBS Cipher
c StrictByteString
key StrictByteString
iv CryptoMode
mode ByteString
input
= do ctx <- Cipher
-> StrictByteString
-> StrictByteString
-> CryptoMode
-> IO CipherCtx
cipherInitBS Cipher
c StrictByteString
key StrictByteString
iv CryptoMode
mode
cipherLazily ctx input