apply zero-padding to miyaguchi-preneel.

This commit is contained in:
Kei Hibino 2016-06-08 22:26:14 +09:00
parent c2285db4e3
commit ec7e73401f

View File

@ -16,6 +16,7 @@ module Crypto.ConstructHash.MiyaguchiPreneel
import Data.List (foldl') import Data.List (foldl')
import Crypto.Data.Padding (pad, Format (ZERO))
import Crypto.Cipher.Types import Crypto.Cipher.Types
import Crypto.Error (throwCryptoError) import Crypto.Error (throwCryptoError)
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray, Bytes) import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray, Bytes)
@ -34,12 +35,12 @@ compute' :: (ByteArrayAccess bin, BlockCipher cipher)
=> (Bytes -> cipher) -- ^ key build function to compute Miyaguchi-Preneel. care about block-size and key-size => (Bytes -> cipher) -- ^ key build function to compute Miyaguchi-Preneel. care about block-size and key-size
-> bin -- ^ input message -> bin -- ^ input message
-> MiyaguchiPreneel cipher -- ^ output tag -> MiyaguchiPreneel cipher -- ^ output tag
compute' g = MP . foldl' (step $ g) (B.replicate bsz 0) . chunks . B.convert compute' g = MP . foldl' (step $ g) (B.replicate bsz 0) . chunks . pad (ZERO bsz) . B.convert
where where
bsz = blockSize ( g B.empty {- dummy to get block size -} ) bsz = blockSize ( g B.empty {- dummy to get block size -} )
chunks msg chunks msg
| B.null tl = [hd :: Bytes] | B.null msg = []
| otherwise = hd : chunks tl | otherwise = (hd :: Bytes) : chunks tl
where where
(hd, tl) = B.splitAt bsz msg (hd, tl) = B.splitAt bsz msg
@ -59,13 +60,9 @@ step :: (ByteArray ba, BlockCipher k)
-> ba -> ba
-> ba -> ba
step g iv msg = step g iv msg =
ecbEncrypt k pmsg `bxor` iv `bxor` pmsg ecbEncrypt k msg `bxor` iv `bxor` msg
where where
k = g iv k = g iv
pmsg = pad0 k msg
pad0 :: (ByteArray ba, BlockCipher k) => k -> ba -> ba
pad0 k s = s `B.append` B.replicate (blockSize k - B.length s) 0
bxor :: ByteArray ba => ba -> ba -> ba bxor :: ByteArray ba => ba -> ba -> ba
bxor = B.xor bxor = B.xor