[Cipher] make xtsGFMul work on arbitrary ByteArray

This commit is contained in:
Vincent Hanquez 2015-04-12 06:50:54 +01:00
parent 796bbef3c2
commit d6f2f7c1c0

View File

@ -14,10 +14,7 @@ module Crypto.Cipher.Types.GF
) where
import Control.Applicative
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Internal as B
import Data.Byteable
import Crypto.Internal.ByteArray
import Foreign.Storable
import Foreign.Ptr
import Data.Word
@ -26,14 +23,15 @@ import Data.Bits
-- block size need to be 128 bits.
--
-- FIXME: add support for big endian.
xtsGFMul :: ByteString -> ByteString
xtsGFMul :: ByteArray ba => ba -> ba
xtsGFMul b
| B.length b == 16 = B.unsafeCreate (B.length b) $ \dst ->
withBytePtr b $ \src -> do
(hi,lo) <- gf <$> peek (castPtr src) <*> peek (castPtr src `plusPtr` 8)
poke (castPtr dst) lo
poke (castPtr dst `plusPtr` 8) hi
| otherwise = error "unsupported block size in GF"
| len == 16 =
byteArrayAllocAndFreeze len $ \dst ->
withByteArray b $ \src -> do
(hi,lo) <- gf <$> peek (castPtr src) <*> peek (castPtr src `plusPtr` 8)
poke (castPtr dst) lo
poke (castPtr dst `plusPtr` 8) hi
| otherwise = error "unsupported block size in GF"
where gf :: Word64 -> Word64 -> (Word64, Word64)
gf srcLo srcHi =
((if carryLo then (.|. 1) else id) (srcHi `shiftL` 1)
@ -41,6 +39,7 @@ xtsGFMul b
)
where carryHi = srcHi `testBit` 63
carryLo = srcLo `testBit` 63
len = byteArrayLength b
{-
const uint64_t gf_mask = cpu_to_le64(0x8000000000000000ULL);
uint64_t r = ((a->q[1] & gf_mask) ? cpu_to_le64(0x87) : 0);