diff --git a/Crypto/Data/Padding.hs b/Crypto/Data/Padding.hs index 095a2de..8bea760 100644 --- a/Crypto/Data/Padding.hs +++ b/Crypto/Data/Padding.hs @@ -21,6 +21,7 @@ import qualified Data.ByteArray as B data Format = PKCS5 -- ^ PKCS5: PKCS7 with hardcoded size of 8 | PKCS7 Int -- ^ PKCS7 with padding size between 1 and 255 + | ZERO Int -- ^ zero padding with block size deriving (Show, Eq) -- | Apply some pad to a bytearray @@ -30,6 +31,15 @@ pad (PKCS7 sz) bin = bin `B.append` paddingString where paddingString = B.replicate paddingByte (fromIntegral paddingByte) paddingByte = sz - (B.length bin `mod` sz) +pad (ZERO sz) bin = bin `B.append` paddingString + where + paddingString = B.replicate paddingSz 0 + paddingSz + | len == 0 = sz + | m == 0 = 0 + | otherwise = sz - m + m = len `mod` sz + len = B.length bin -- | Try to remove some padding from a bytearray. unpad :: ByteArray byteArray => Format -> byteArray -> Maybe byteArray @@ -46,3 +56,4 @@ unpad (PKCS7 sz) bin paddingSz = fromIntegral paddingByte (content, padding) = B.splitAt (len - paddingSz) bin paddingWitness = B.replicate paddingSz paddingByte :: Bytes +unpad (ZERO sz) bin = Nothing diff --git a/tests/Padding.hs b/tests/Padding.hs index 7a6e7fd..a3af7cb 100644 --- a/tests/Padding.hs +++ b/tests/Padding.hs @@ -13,6 +13,12 @@ cases = , ("xyze", 5, "xyze\x01") ] +zeroCases = + [ ("", 4, "\NUL\NUL\NUL\NUL") + , ("abcdef", 8, "abcdef\NUL\NUL") + , ("0123456789abcdef", 16, "0123456789abcdef") + ] + --instance Arbitrary where testPad :: Int -> (B.ByteString, Int, B.ByteString) -> TestTree @@ -21,6 +27,11 @@ testPad n (inp, sz, padded) = , eqTest "unpadded" (Just inp) (unpad (PKCS7 sz) padded) ] +testZeroPad :: Int -> (B.ByteString, Int, B.ByteString) -> TestTree +testZeroPad n (inp, sz, padded) = + testCase (show n) $ propertyHoldCase [ eqTest "padded" padded (pad (ZERO sz) inp) ] + tests = testGroup "Padding" [ testGroup "Cases" $ map (uncurry testPad) (zip [1..] cases) + , testGroup "ZeroCases" $ map (uncurry testZeroPad) (zip [1..] zeroCases) ]