{-# LANGUAGE CPP #-}
{-# LANGUAGE ForeignFunctionInterface #-}
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 ccall unsafe "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 EVP_CIPHER
ptr <- CString -> IO (Ptr EVP_CIPHER)
_get_cipherbyname CString
namePtr
if Ptr EVP_CIPHER
ptr Ptr EVP_CIPHER -> Ptr EVP_CIPHER -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr EVP_CIPHER
forall a. Ptr a
nullPtr then
Maybe Cipher -> IO (Maybe Cipher)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Cipher
forall a. Maybe a
Nothing
else
Maybe Cipher -> IO (Maybe Cipher)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Cipher -> IO (Maybe Cipher))
-> Maybe Cipher -> IO (Maybe Cipher)
forall a b. (a -> b) -> a -> b
$ Cipher -> Maybe Cipher
forall a. a -> Maybe a
Just (Cipher -> Maybe Cipher) -> Cipher -> Maybe Cipher
forall a b. (a -> b) -> a -> b
$ Ptr EVP_CIPHER -> Cipher
Cipher Ptr EVP_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
-> ByteString
-> ByteString
-> CryptoMode
-> ByteString
-> IO ByteString
cipherStrictLBS Cipher
c ByteString
key ByteString
iv CryptoMode
mode ByteString
input =
do CipherCtx
ctx <- Cipher -> ByteString -> ByteString -> CryptoMode -> IO CipherCtx
cipherInitBS Cipher
c ByteString
key ByteString
iv CryptoMode
mode
[ByteString]
xs <- CipherCtx -> ByteString -> IO ByteString
cipherUpdateBS CipherCtx
ctx (ByteString -> IO ByteString) -> [ByteString] -> IO [ByteString]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
`mapM` ByteString -> [ByteString]
L8.toChunks ByteString
input
ByteString
x <- CipherCtx -> IO ByteString
cipherFinalBS CipherCtx
ctx
ByteString -> IO ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> IO ByteString) -> ByteString -> IO ByteString
forall a b. (a -> b) -> a -> b
$ [ByteString] -> ByteString
L8.fromChunks ([ByteString]
xs [ByteString] -> [ByteString] -> [ByteString]
forall a. Monoid a => a -> a -> a
`mappend` [ByteString
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 (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
-> ByteString
-> ByteString
-> CryptoMode
-> ByteString
-> IO ByteString
cipherLBS Cipher
c (String -> ByteString
B8.pack String
key) (String -> ByteString
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
-> ByteString
-> ByteString
-> CryptoMode
-> ByteString
-> IO ByteString
cipherBS Cipher
c ByteString
key ByteString
iv CryptoMode
mode ByteString
input
= do CipherCtx
ctx <- Cipher -> ByteString -> ByteString -> CryptoMode -> IO CipherCtx
cipherInitBS Cipher
c ByteString
key ByteString
iv CryptoMode
mode
CipherCtx -> ByteString -> IO ByteString
cipherStrictly CipherCtx
ctx ByteString
input
cipherLBS :: Cipher
-> B8.ByteString
-> B8.ByteString
-> CryptoMode
-> L8.ByteString
-> IO L8.ByteString
cipherLBS :: Cipher
-> ByteString
-> ByteString
-> CryptoMode
-> ByteString
-> IO ByteString
cipherLBS Cipher
c ByteString
key ByteString
iv CryptoMode
mode ByteString
input
= do CipherCtx
ctx <- Cipher -> ByteString -> ByteString -> CryptoMode -> IO CipherCtx
cipherInitBS Cipher
c ByteString
key ByteString
iv CryptoMode
mode
CipherCtx -> ByteString -> IO ByteString
cipherLazily CipherCtx
ctx ByteString
input