{-# LANGUAGE RankNTypes #-}
module Codec.Encryption.OpenPGP.BlockCipher (
keySize
, withSymmetricCipher
) where
import Codec.Encryption.OpenPGP.Internal.CryptoCipherTypes (HOWrappedOldCCT(..))
import Codec.Encryption.OpenPGP.Internal.Cryptonite (HOWrappedCCT(..))
import Codec.Encryption.OpenPGP.Internal.HOBlockCipher
import Codec.Encryption.OpenPGP.Types
import qualified Crypto.Cipher.Blowfish as Blowfish
import qualified Crypto.Cipher.TripleDES as TripleDES
import qualified Crypto.Cipher.AES as AES
import qualified Crypto.Cipher.Camellia as Camellia
import qualified Crypto.Nettle.Ciphers as CNC
import qualified Data.ByteString as B
type HOCipher a = forall cipher. HOBlockCipher cipher => cipher -> Either String a
withSymmetricCipher :: SymmetricAlgorithm -> B.ByteString -> HOCipher a -> Either String a
withSymmetricCipher Plaintext _ _ = Left "this shouldn't have happened"
withSymmetricCipher IDEA _ _ = Left "IDEA not yet implemented"
withSymmetricCipher ReservedSAFER _ _ = Left "SAFER not implemented"
withSymmetricCipher ReservedDES _ _ = Left "DES not implemented"
withSymmetricCipher (OtherSA _) _ _ = Left "Unknown, unimplemented symmetric algorithm"
withSymmetricCipher CAST5 key f = (cipherInit key :: Either String (HOWrappedOldCCT CNC.CAST128)) >>= f
withSymmetricCipher Twofish key f = (cipherInit key :: Either String (HOWrappedOldCCT CNC.TWOFISH)) >>= f
withSymmetricCipher TripleDES key f = (cipherInit key :: Either String (HOWrappedCCT TripleDES.DES_EDE3)) >>= f
withSymmetricCipher Blowfish key f = (cipherInit key :: Either String (HOWrappedCCT Blowfish.Blowfish128)) >>= f
withSymmetricCipher AES128 key f = (cipherInit key :: Either String (HOWrappedCCT AES.AES128)) >>= f
withSymmetricCipher AES192 key f = (cipherInit key :: Either String (HOWrappedCCT AES.AES192)) >>= f
withSymmetricCipher AES256 key f = (cipherInit key :: Either String (HOWrappedCCT AES.AES256)) >>= f
withSymmetricCipher Camellia128 key f = (cipherInit key :: Either String (HOWrappedCCT Camellia.Camellia128)) >>= f
withSymmetricCipher Camellia192 key f = (cipherInit key :: Either String (HOWrappedOldCCT CNC.Camellia192)) >>= f
withSymmetricCipher Camellia256 key f = (cipherInit key :: Either String (HOWrappedOldCCT CNC.Camellia256)) >>= f
keySize :: SymmetricAlgorithm -> Int
keySize Plaintext = 0
keySize IDEA = 16
keySize TripleDES = 24
keySize CAST5 = 16
keySize Blowfish = 16
keySize ReservedSAFER = undefined
keySize ReservedDES = undefined
keySize AES128 = 16
keySize AES192 = 24
keySize AES256 = 32
keySize Twofish = 32
keySize Camellia128 = 16
keySize Camellia192 = 24
keySize Camellia256 = 32
keySize (OtherSA _) = undefined