{-# LANGUAGE RankNTypes #-}
module Codec.Encryption.OpenPGP.BlockCipher
( CipherError(..)
, renderCipherError
, keySize
, withSymmetricCipher
) where
import Codec.Encryption.OpenPGP.Internal.CryptoCipherTypes (HOWrappedOldCCT(..))
import Codec.Encryption.OpenPGP.Internal.Crypton (HOWrappedCCT(..))
import Codec.Encryption.OpenPGP.Internal.HOBlockCipher
import Codec.Encryption.OpenPGP.Types
import qualified Crypto.Cipher.AES as AES
import qualified Crypto.Cipher.Blowfish as Blowfish
import qualified Crypto.Cipher.Camellia as Camellia
import qualified Crypto.Cipher.TripleDES as TripleDES
import qualified Crypto.Nettle.Ciphers as CNC
import qualified Data.ByteString as B
data CipherError
=
UnsupportedAlgorithm SymmetricAlgorithm
|
CipherInitFailed SymmetricAlgorithm String
|
CipherOperationFailed String
deriving (CipherError -> CipherError -> Bool
(CipherError -> CipherError -> Bool)
-> (CipherError -> CipherError -> Bool) -> Eq CipherError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CipherError -> CipherError -> Bool
== :: CipherError -> CipherError -> Bool
$c/= :: CipherError -> CipherError -> Bool
/= :: CipherError -> CipherError -> Bool
Eq, Int -> CipherError -> ShowS
[CipherError] -> ShowS
CipherError -> String
(Int -> CipherError -> ShowS)
-> (CipherError -> String)
-> ([CipherError] -> ShowS)
-> Show CipherError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CipherError -> ShowS
showsPrec :: Int -> CipherError -> ShowS
$cshow :: CipherError -> String
show :: CipherError -> String
$cshowList :: [CipherError] -> ShowS
showList :: [CipherError] -> ShowS
Show)
renderCipherError :: CipherError -> String
renderCipherError :: CipherError -> String
renderCipherError (UnsupportedAlgorithm SymmetricAlgorithm
sa) =
String
"Unsupported symmetric algorithm: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SymmetricAlgorithm -> String
forall a. Show a => a -> String
show SymmetricAlgorithm
sa
renderCipherError (CipherInitFailed SymmetricAlgorithm
sa String
msg) =
String
"Cipher initialization failed for " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SymmetricAlgorithm -> String
forall a. Show a => a -> String
show SymmetricAlgorithm
sa String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
": " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
msg
renderCipherError (CipherOperationFailed String
msg) =
String
"Cipher operation failed: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
msg
type HOCipher a
= forall cipher. HOBlockCipher cipher =>
cipher -> Either String a
withSymmetricCipher ::
SymmetricAlgorithm -> B.ByteString -> HOCipher a -> Either CipherError a
withSymmetricCipher :: forall a.
SymmetricAlgorithm
-> ByteString -> HOCipher a -> Either CipherError a
withSymmetricCipher SymmetricAlgorithm
Plaintext ByteString
_ HOCipher a
_ = CipherError -> Either CipherError a
forall a b. a -> Either a b
Left (SymmetricAlgorithm -> CipherError
UnsupportedAlgorithm SymmetricAlgorithm
Plaintext)
withSymmetricCipher SymmetricAlgorithm
IDEA ByteString
_ HOCipher a
_ = CipherError -> Either CipherError a
forall a b. a -> Either a b
Left (SymmetricAlgorithm -> CipherError
UnsupportedAlgorithm SymmetricAlgorithm
IDEA)
withSymmetricCipher SymmetricAlgorithm
ReservedSAFER ByteString
_ HOCipher a
_ = CipherError -> Either CipherError a
forall a b. a -> Either a b
Left (SymmetricAlgorithm -> CipherError
UnsupportedAlgorithm SymmetricAlgorithm
ReservedSAFER)
withSymmetricCipher SymmetricAlgorithm
ReservedDES ByteString
_ HOCipher a
_ = CipherError -> Either CipherError a
forall a b. a -> Either a b
Left (SymmetricAlgorithm -> CipherError
UnsupportedAlgorithm SymmetricAlgorithm
ReservedDES)
withSymmetricCipher (OtherSA Word8
n) ByteString
_ HOCipher a
_ = CipherError -> Either CipherError a
forall a b. a -> Either a b
Left (SymmetricAlgorithm -> CipherError
UnsupportedAlgorithm (Word8 -> SymmetricAlgorithm
OtherSA Word8
n))
withSymmetricCipher SymmetricAlgorithm
CAST5 ByteString
keyBytes HOCipher a
f =
SymmetricAlgorithm
-> Either String (HOWrappedOldCCT CAST128)
-> (HOWrappedOldCCT CAST128 -> Either String a)
-> Either CipherError a
forall cipher a.
HOBlockCipher cipher =>
SymmetricAlgorithm
-> Either String cipher
-> (cipher -> Either String a)
-> Either CipherError a
initAndRun SymmetricAlgorithm
CAST5 (ByteString -> Either String (HOWrappedOldCCT CAST128)
forall cipher.
HOBlockCipher cipher =>
ByteString -> Either String cipher
cipherInit ByteString
keyBytes :: Either String (HOWrappedOldCCT CNC.CAST128)) HOWrappedOldCCT CAST128 -> Either String a
HOCipher a
f
withSymmetricCipher SymmetricAlgorithm
Twofish ByteString
keyBytes HOCipher a
f =
SymmetricAlgorithm
-> Either String (HOWrappedOldCCT TWOFISH)
-> (HOWrappedOldCCT TWOFISH -> Either String a)
-> Either CipherError a
forall cipher a.
HOBlockCipher cipher =>
SymmetricAlgorithm
-> Either String cipher
-> (cipher -> Either String a)
-> Either CipherError a
initAndRun SymmetricAlgorithm
Twofish (ByteString -> Either String (HOWrappedOldCCT TWOFISH)
forall cipher.
HOBlockCipher cipher =>
ByteString -> Either String cipher
cipherInit ByteString
keyBytes :: Either String (HOWrappedOldCCT CNC.TWOFISH)) HOWrappedOldCCT TWOFISH -> Either String a
HOCipher a
f
withSymmetricCipher SymmetricAlgorithm
TripleDES ByteString
keyBytes HOCipher a
f =
SymmetricAlgorithm
-> Either String (HOWrappedCCT DES_EDE3)
-> (HOWrappedCCT DES_EDE3 -> Either String a)
-> Either CipherError a
forall cipher a.
HOBlockCipher cipher =>
SymmetricAlgorithm
-> Either String cipher
-> (cipher -> Either String a)
-> Either CipherError a
initAndRun SymmetricAlgorithm
TripleDES (ByteString -> Either String (HOWrappedCCT DES_EDE3)
forall cipher.
HOBlockCipher cipher =>
ByteString -> Either String cipher
cipherInit ByteString
keyBytes :: Either String (HOWrappedCCT TripleDES.DES_EDE3)) HOWrappedCCT DES_EDE3 -> Either String a
HOCipher a
f
withSymmetricCipher SymmetricAlgorithm
Blowfish ByteString
keyBytes HOCipher a
f =
SymmetricAlgorithm
-> Either String (HOWrappedCCT Blowfish128)
-> (HOWrappedCCT Blowfish128 -> Either String a)
-> Either CipherError a
forall cipher a.
HOBlockCipher cipher =>
SymmetricAlgorithm
-> Either String cipher
-> (cipher -> Either String a)
-> Either CipherError a
initAndRun SymmetricAlgorithm
Blowfish (ByteString -> Either String (HOWrappedCCT Blowfish128)
forall cipher.
HOBlockCipher cipher =>
ByteString -> Either String cipher
cipherInit ByteString
keyBytes :: Either String (HOWrappedCCT Blowfish.Blowfish128)) HOWrappedCCT Blowfish128 -> Either String a
HOCipher a
f
withSymmetricCipher SymmetricAlgorithm
AES128 ByteString
keyBytes HOCipher a
f =
SymmetricAlgorithm
-> Either String (HOWrappedCCT AES128)
-> (HOWrappedCCT AES128 -> Either String a)
-> Either CipherError a
forall cipher a.
HOBlockCipher cipher =>
SymmetricAlgorithm
-> Either String cipher
-> (cipher -> Either String a)
-> Either CipherError a
initAndRun SymmetricAlgorithm
AES128 (ByteString -> Either String (HOWrappedCCT AES128)
forall cipher.
HOBlockCipher cipher =>
ByteString -> Either String cipher
cipherInit ByteString
keyBytes :: Either String (HOWrappedCCT AES.AES128)) HOWrappedCCT AES128 -> Either String a
HOCipher a
f
withSymmetricCipher SymmetricAlgorithm
AES192 ByteString
keyBytes HOCipher a
f =
SymmetricAlgorithm
-> Either String (HOWrappedCCT AES192)
-> (HOWrappedCCT AES192 -> Either String a)
-> Either CipherError a
forall cipher a.
HOBlockCipher cipher =>
SymmetricAlgorithm
-> Either String cipher
-> (cipher -> Either String a)
-> Either CipherError a
initAndRun SymmetricAlgorithm
AES192 (ByteString -> Either String (HOWrappedCCT AES192)
forall cipher.
HOBlockCipher cipher =>
ByteString -> Either String cipher
cipherInit ByteString
keyBytes :: Either String (HOWrappedCCT AES.AES192)) HOWrappedCCT AES192 -> Either String a
HOCipher a
f
withSymmetricCipher SymmetricAlgorithm
AES256 ByteString
keyBytes HOCipher a
f =
SymmetricAlgorithm
-> Either String (HOWrappedCCT AES256)
-> (HOWrappedCCT AES256 -> Either String a)
-> Either CipherError a
forall cipher a.
HOBlockCipher cipher =>
SymmetricAlgorithm
-> Either String cipher
-> (cipher -> Either String a)
-> Either CipherError a
initAndRun SymmetricAlgorithm
AES256 (ByteString -> Either String (HOWrappedCCT AES256)
forall cipher.
HOBlockCipher cipher =>
ByteString -> Either String cipher
cipherInit ByteString
keyBytes :: Either String (HOWrappedCCT AES.AES256)) HOWrappedCCT AES256 -> Either String a
HOCipher a
f
withSymmetricCipher SymmetricAlgorithm
Camellia128 ByteString
keyBytes HOCipher a
f =
SymmetricAlgorithm
-> Either String (HOWrappedCCT Camellia128)
-> (HOWrappedCCT Camellia128 -> Either String a)
-> Either CipherError a
forall cipher a.
HOBlockCipher cipher =>
SymmetricAlgorithm
-> Either String cipher
-> (cipher -> Either String a)
-> Either CipherError a
initAndRun SymmetricAlgorithm
Camellia128 (ByteString -> Either String (HOWrappedCCT Camellia128)
forall cipher.
HOBlockCipher cipher =>
ByteString -> Either String cipher
cipherInit ByteString
keyBytes :: Either String (HOWrappedCCT Camellia.Camellia128)) HOWrappedCCT Camellia128 -> Either String a
HOCipher a
f
withSymmetricCipher SymmetricAlgorithm
Camellia192 ByteString
keyBytes HOCipher a
f =
SymmetricAlgorithm
-> Either String (HOWrappedOldCCT Camellia192)
-> (HOWrappedOldCCT Camellia192 -> Either String a)
-> Either CipherError a
forall cipher a.
HOBlockCipher cipher =>
SymmetricAlgorithm
-> Either String cipher
-> (cipher -> Either String a)
-> Either CipherError a
initAndRun SymmetricAlgorithm
Camellia192 (ByteString -> Either String (HOWrappedOldCCT Camellia192)
forall cipher.
HOBlockCipher cipher =>
ByteString -> Either String cipher
cipherInit ByteString
keyBytes :: Either String (HOWrappedOldCCT CNC.Camellia192)) HOWrappedOldCCT Camellia192 -> Either String a
HOCipher a
f
withSymmetricCipher SymmetricAlgorithm
Camellia256 ByteString
keyBytes HOCipher a
f =
SymmetricAlgorithm
-> Either String (HOWrappedOldCCT Camellia256)
-> (HOWrappedOldCCT Camellia256 -> Either String a)
-> Either CipherError a
forall cipher a.
HOBlockCipher cipher =>
SymmetricAlgorithm
-> Either String cipher
-> (cipher -> Either String a)
-> Either CipherError a
initAndRun SymmetricAlgorithm
Camellia256 (ByteString -> Either String (HOWrappedOldCCT Camellia256)
forall cipher.
HOBlockCipher cipher =>
ByteString -> Either String cipher
cipherInit ByteString
keyBytes :: Either String (HOWrappedOldCCT CNC.Camellia256)) HOWrappedOldCCT Camellia256 -> Either String a
HOCipher a
f
initAndRun ::
HOBlockCipher cipher
=> SymmetricAlgorithm
-> Either String cipher
-> (cipher -> Either String a)
-> Either CipherError a
initAndRun :: forall cipher a.
HOBlockCipher cipher =>
SymmetricAlgorithm
-> Either String cipher
-> (cipher -> Either String a)
-> Either CipherError a
initAndRun SymmetricAlgorithm
algo Either String cipher
initResult cipher -> Either String a
f =
case Either String cipher
initResult of
Left String
err -> CipherError -> Either CipherError a
forall a b. a -> Either a b
Left (SymmetricAlgorithm -> String -> CipherError
CipherInitFailed SymmetricAlgorithm
algo String
err)
Right cipher
c ->
case cipher -> Either String a
f cipher
c of
Left String
err -> CipherError -> Either CipherError a
forall a b. a -> Either a b
Left (String -> CipherError
CipherOperationFailed String
err)
Right a
x -> a -> Either CipherError a
forall a b. b -> Either a b
Right a
x
keySize :: SymmetricAlgorithm -> Either CipherError Int
keySize :: SymmetricAlgorithm -> Either CipherError Int
keySize SymmetricAlgorithm
Plaintext = Int -> Either CipherError Int
forall a b. b -> Either a b
Right Int
0
keySize SymmetricAlgorithm
IDEA = Int -> Either CipherError Int
forall a b. b -> Either a b
Right Int
16
keySize SymmetricAlgorithm
TripleDES = Int -> Either CipherError Int
forall a b. b -> Either a b
Right Int
24
keySize SymmetricAlgorithm
CAST5 = Int -> Either CipherError Int
forall a b. b -> Either a b
Right Int
16
keySize SymmetricAlgorithm
Blowfish = Int -> Either CipherError Int
forall a b. b -> Either a b
Right Int
16
keySize SymmetricAlgorithm
ReservedSAFER = CipherError -> Either CipherError Int
forall a b. a -> Either a b
Left (SymmetricAlgorithm -> CipherError
UnsupportedAlgorithm SymmetricAlgorithm
ReservedSAFER)
keySize SymmetricAlgorithm
ReservedDES = CipherError -> Either CipherError Int
forall a b. a -> Either a b
Left (SymmetricAlgorithm -> CipherError
UnsupportedAlgorithm SymmetricAlgorithm
ReservedDES)
keySize SymmetricAlgorithm
AES128 = Int -> Either CipherError Int
forall a b. b -> Either a b
Right Int
16
keySize SymmetricAlgorithm
AES192 = Int -> Either CipherError Int
forall a b. b -> Either a b
Right Int
24
keySize SymmetricAlgorithm
AES256 = Int -> Either CipherError Int
forall a b. b -> Either a b
Right Int
32
keySize SymmetricAlgorithm
Twofish = Int -> Either CipherError Int
forall a b. b -> Either a b
Right Int
32
keySize SymmetricAlgorithm
Camellia128 = Int -> Either CipherError Int
forall a b. b -> Either a b
Right Int
16
keySize SymmetricAlgorithm
Camellia192 = Int -> Either CipherError Int
forall a b. b -> Either a b
Right Int
24
keySize SymmetricAlgorithm
Camellia256 = Int -> Either CipherError Int
forall a b. b -> Either a b
Right Int
32
keySize (OtherSA Word8
n) = CipherError -> Either CipherError Int
forall a b. a -> Either a b
Left (SymmetricAlgorithm -> CipherError
UnsupportedAlgorithm (Word8 -> SymmetricAlgorithm
OtherSA Word8
n))