From d6f2f7c1c0c809952cbcb3cfb4602059d347a447 Mon Sep 17 00:00:00 2001 From: Vincent Hanquez Date: Sun, 12 Apr 2015 06:50:54 +0100 Subject: [PATCH] [Cipher] make xtsGFMul work on arbitrary ByteArray --- Crypto/Cipher/Types/GF.hs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Crypto/Cipher/Types/GF.hs b/Crypto/Cipher/Types/GF.hs index 647a973..2fa48e2 100644 --- a/Crypto/Cipher/Types/GF.hs +++ b/Crypto/Cipher/Types/GF.hs @@ -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);