Skip to content

Commit

Permalink
Fix blowfish
Browse files Browse the repository at this point in the history
  • Loading branch information
singpolyma committed Jan 5, 2013
1 parent 5d8f8ec commit c471b14
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
14 changes: 7 additions & 7 deletions Data/OpenPGP/CryptoAPI.hs
Expand Up @@ -22,7 +22,6 @@ import Crypto.Hash.SHA384 (SHA384)
import Crypto.Hash.SHA512 (SHA512)
import Crypto.Hash.SHA224 (SHA224)
import Crypto.Cipher.AES (AES128,AES192,AES256)
import Crypto.Cipher.Blowfish (Blowfish)
import qualified Data.Serialize as Serialize
import qualified Crypto.Cipher.RSA as RSA
import qualified Crypto.Cipher.DSA as DSA
Expand All @@ -32,6 +31,7 @@ import qualified Data.ByteString.Lazy.UTF8 as LZ (fromString)

import qualified Data.OpenPGP as OpenPGP
import Data.OpenPGP.CryptoAPI.Util
import Data.OpenPGP.CryptoAPI.Blowfish128

-- | An encryption routine
type Encrypt g = (LZ.ByteString -> g -> (LZ.ByteString, g))
Expand Down Expand Up @@ -343,7 +343,7 @@ s2kHashAlgorithmFor :: OpenPGP.SymmetricAlgorithm -> OpenPGP.HashAlgorithm
s2kHashAlgorithmFor OpenPGP.AES128 = s2kHashAlgorithm `for` (undefined :: AES128)
s2kHashAlgorithmFor OpenPGP.AES192 = s2kHashAlgorithm `for` (undefined :: AES192)
s2kHashAlgorithmFor OpenPGP.AES256 = s2kHashAlgorithm `for` (undefined :: AES256)
s2kHashAlgorithmFor OpenPGP.Blowfish = s2kHashAlgorithm `for` (undefined :: Blowfish)
s2kHashAlgorithmFor OpenPGP.Blowfish = s2kHashAlgorithm `for` (undefined :: Blowfish128)
s2kHashAlgorithmFor algo = error $ "Unsupported SymmetricAlgorithm " ++ show algo ++ " in Data.OpenPGP.CryptoAPI.s2kHashAlgorithmFor"

s2kHashAlgorithm :: (BlockCipher k) => Tagged k OpenPGP.HashAlgorithm
Expand Down Expand Up @@ -373,7 +373,7 @@ sessionFor algo@OpenPGP.AES256 msg = do
return (sessionKeyEncode sk algo, encP)
sessionFor algo@OpenPGP.Blowfish msg = do
sk <- StateT buildKeyGen
encP <- newSession (sk :: Blowfish) msg
encP <- newSession (sk :: Blowfish128) msg
return (sessionKeyEncode sk algo, encP)
sessionFor algo _ = lift $ Left $ GenErrorOther $ "Unsupported cipher: " ++ show algo

Expand Down Expand Up @@ -517,28 +517,28 @@ decodeSymKey :: OpenPGP.SymmetricAlgorithm -> BS.ByteString -> Maybe Decrypt
decodeSymKey OpenPGP.AES128 k = pgpUnCFB <$> (`asTypeOf` (undefined :: AES128)) <$> sDecode k
decodeSymKey OpenPGP.AES192 k = pgpUnCFB <$> (`asTypeOf` (undefined :: AES192)) <$> sDecode k
decodeSymKey OpenPGP.AES256 k = pgpUnCFB <$> (`asTypeOf` (undefined :: AES256)) <$> sDecode k
decodeSymKey OpenPGP.Blowfish k = pgpUnCFB <$> (`asTypeOf` (undefined :: Blowfish)) <$> sDecode k
decodeSymKey OpenPGP.Blowfish k = pgpUnCFB <$> (`asTypeOf` (undefined :: Blowfish128)) <$> sDecode k
decodeSymKey _ _ = Nothing

string2sencrypt :: OpenPGP.SymmetricAlgorithm -> OpenPGP.S2K -> LZ.ByteString -> LZ.ByteString -> LZ.ByteString
string2sencrypt OpenPGP.AES128 s2k s = simpleCFB (string2key s2k s :: AES128) zeroIV
string2sencrypt OpenPGP.AES192 s2k s = simpleCFB (string2key s2k s :: AES192) zeroIV
string2sencrypt OpenPGP.AES256 s2k s = simpleCFB (string2key s2k s :: AES256) zeroIV
string2sencrypt OpenPGP.Blowfish s2k s = simpleCFB (string2key s2k s :: Blowfish) zeroIV
string2sencrypt OpenPGP.Blowfish s2k s = simpleCFB (string2key s2k s :: Blowfish128) zeroIV
string2sencrypt algo _ _ = error $ "Unsupported symmetric algorithm : " ++ show algo ++ " in Data.OpenPGP.CryptoAPI.string2decrypt"

string2decrypt :: OpenPGP.SymmetricAlgorithm -> OpenPGP.S2K -> LZ.ByteString -> Decrypt
string2decrypt OpenPGP.AES128 s2k s = pgpUnCFB (string2key s2k s :: AES128)
string2decrypt OpenPGP.AES192 s2k s = pgpUnCFB (string2key s2k s :: AES192)
string2decrypt OpenPGP.AES256 s2k s = pgpUnCFB (string2key s2k s :: AES256)
string2decrypt OpenPGP.Blowfish s2k s = pgpUnCFB (string2key s2k s :: Blowfish)
string2decrypt OpenPGP.Blowfish s2k s = pgpUnCFB (string2key s2k s :: Blowfish128)
string2decrypt algo _ _ = error $ "Unsupported symmetric algorithm : " ++ show algo ++ " in Data.OpenPGP.CryptoAPI.string2decrypt"

string2sdecrypt :: OpenPGP.SymmetricAlgorithm -> OpenPGP.S2K -> LZ.ByteString -> Enciphered -> LZ.ByteString
string2sdecrypt OpenPGP.AES128 s2k s = withIV $ simpleUnCFB (string2key s2k s :: AES128)
string2sdecrypt OpenPGP.AES192 s2k s = withIV $ simpleUnCFB (string2key s2k s :: AES192)
string2sdecrypt OpenPGP.AES256 s2k s = withIV $ simpleUnCFB (string2key s2k s :: AES256)
string2sdecrypt OpenPGP.Blowfish s2k s = withIV $ simpleUnCFB (string2key s2k s :: Blowfish)
string2sdecrypt OpenPGP.Blowfish s2k s = withIV $ simpleUnCFB (string2key s2k s :: Blowfish128)
string2sdecrypt algo _ _ = error $ "Unsupported symmetric algorithm : " ++ show algo ++ " in Data.OpenPGP.CryptoAPI.string2sdecrypt"

data Enciphered = EncipheredWithIV !LZ.ByteString | EncipheredZeroIV !LZ.ByteString
Expand Down
20 changes: 20 additions & 0 deletions Data/OpenPGP/CryptoAPI/Blowfish128.hs
@@ -0,0 +1,20 @@
module Data.OpenPGP.CryptoAPI.Blowfish128 (Blowfish128) where

import Crypto.Classes (BlockCipher(..))
import Crypto.Types (BitLength)
import Crypto.Cipher.Blowfish (Blowfish)
import Data.Tagged (retag, Tagged(..))
import qualified Data.Serialize as Serialize

newtype Blowfish128 = Blowfish128 Blowfish

instance Serialize.Serialize Blowfish128 where
put (Blowfish128 b) = Serialize.put b
get = fmap Blowfish128 Serialize.get

instance BlockCipher Blowfish128 where
blockSize = retag (blockSize :: Tagged Blowfish BitLength)
encryptBlock (Blowfish128 k) = encryptBlock k
decryptBlock (Blowfish128 k) = decryptBlock k
buildKey = fmap Blowfish128 . buildKey
keyLength = Tagged 128
File renamed without changes.
1 change: 1 addition & 0 deletions tests/data/symmetric-blowfish.gpg
@@ -0,0 +1 @@
�B|Lɩ<��`�2��G5��z���g������%d���`�~JX�M��ľm˦��4�
Expand Down
File renamed without changes.
5 changes: 3 additions & 2 deletions tests/suite.hs
Expand Up @@ -116,8 +116,9 @@ tests secring oneKey rng =
],
testGroup "Decryption" [
testCase "decrypt hello" testDecryptHello,
testCase "decrypt PGP" (testDecryptSymmetric "hello" "PGP\n" "symmetric.gpg"),
testCase "decrypt PGP" (testDecryptSymmetric "hello" "PGP\n" "symmetric2.gpg"),
testCase "decrypt AES" (testDecryptSymmetric "hello" "PGP\n" "symmetric-aes.gpg"),
testCase "decrypt session key" (testDecryptSymmetric "hello" "PGP\n" "symmetric-with-session-key.gpg"),
testCase "decrypt Blowfish" (testDecryptSymmetric "hello" "PGP\n" "symmetric-blowfish.gpg"),
testCase "decrypt secret key" (testDecryptSecretKey "hello" "encryptedSecretKey.gpg")
],
testGroup "Encryption" [
Expand Down

0 comments on commit c471b14

Please sign in to comment.