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