Fix ivAdd overflow behaviour
This commit is contained in:
parent
8fb59dfc19
commit
07592ab237
@ -36,6 +36,7 @@ module Crypto.Cipher.Types.Block
|
|||||||
--, cfb8Decrypt
|
--, cfb8Decrypt
|
||||||
) where
|
) where
|
||||||
|
|
||||||
|
import Control.Monad (unless)
|
||||||
import Data.Word
|
import Data.Word
|
||||||
import Data.Monoid
|
import Data.Monoid
|
||||||
import Crypto.Error
|
import Crypto.Error
|
||||||
@ -167,24 +168,16 @@ nullIV = toIV undefined
|
|||||||
ivAdd :: BlockCipher c => IV c -> Int -> IV c
|
ivAdd :: BlockCipher c => IV c -> Int -> IV c
|
||||||
ivAdd (IV b) i = IV $ copy b
|
ivAdd (IV b) i = IV $ copy b
|
||||||
where copy :: ByteArray bs => bs -> bs
|
where copy :: ByteArray bs => bs -> bs
|
||||||
copy bs = B.copyAndFreeze bs $ \p -> do
|
copy bs = B.copyAndFreeze bs $ loop i (B.length bs - 1)
|
||||||
let until0 accu = do
|
|
||||||
r <- loop accu (B.length bs - 1) p
|
|
||||||
case r of
|
|
||||||
0 -> return ()
|
|
||||||
_ -> until0 r
|
|
||||||
until0 i
|
|
||||||
|
|
||||||
loop :: Int -> Int -> Ptr Word8 -> IO Int
|
loop :: Int -> Int -> Ptr Word8 -> IO ()
|
||||||
loop 0 _ _ = return 0
|
loop 0 _ _ = return ()
|
||||||
loop acc ofs p = do
|
loop acc ofs p = do
|
||||||
v <- peek (p `plusPtr` ofs) :: IO Word8
|
v <- peek (p `plusPtr` ofs) :: IO Word8
|
||||||
let accv = acc + fromIntegral v
|
let accv = acc + fromIntegral v
|
||||||
(hi,lo) = accv `divMod` 256
|
(hi,lo) = accv `divMod` 256
|
||||||
poke (p `plusPtr` ofs) (fromIntegral lo :: Word8)
|
poke (p `plusPtr` ofs) (fromIntegral lo :: Word8)
|
||||||
if ofs == 0
|
unless (ofs == 0) $ loop hi (ofs - 1) p
|
||||||
then return hi
|
|
||||||
else loop hi (ofs - 1) p
|
|
||||||
|
|
||||||
cbcEncryptGeneric :: (ByteArray ba, BlockCipher cipher) => cipher -> IV cipher -> ba -> ba
|
cbcEncryptGeneric :: (ByteArray ba, BlockCipher cipher) => cipher -> IV cipher -> ba -> ba
|
||||||
cbcEncryptGeneric cipher ivini input = mconcat $ doEnc ivini $ chunk (blockSize cipher) input
|
cbcEncryptGeneric cipher ivini input = mconcat $ doEnc ivini $ chunk (blockSize cipher) input
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user