module Crypto.Cipher.Types.GF
(
xtsGFMul
) where
import Crypto.Internal.Imports
import Crypto.Internal.ByteArray (ByteArray, withByteArray)
import qualified Crypto.Internal.ByteArray as B
import Foreign.Storable
import Foreign.Ptr
import Data.Bits
xtsGFMul :: ByteArray ba => ba -> ba
xtsGFMul :: forall ba. ByteArray ba => ba -> ba
xtsGFMul ba
b
| Int
len forall a. Eq a => a -> a -> Bool
== Int
16 =
forall a p. ByteArray a => Int -> (Ptr p -> IO ()) -> a
B.allocAndFreeze Int
len forall a b. (a -> b) -> a -> b
$ \Ptr Any
dst ->
forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray ba
b forall a b. (a -> b) -> a -> b
$ \Ptr Any
src -> do
(Word64
hi,Word64
lo) <- Word64 -> Word64 -> (Word64, Word64)
gf forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Storable a => Ptr a -> IO a
peek (forall a b. Ptr a -> Ptr b
castPtr Ptr Any
src) forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. Storable a => Ptr a -> IO a
peek (forall a b. Ptr a -> Ptr b
castPtr Ptr Any
src forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
8)
forall a. Storable a => Ptr a -> a -> IO ()
poke (forall a b. Ptr a -> Ptr b
castPtr Ptr Any
dst) Word64
lo
forall a. Storable a => Ptr a -> a -> IO ()
poke (forall a b. Ptr a -> Ptr b
castPtr Ptr Any
dst forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
8) Word64
hi
| Bool
otherwise = forall a. HasCallStack => [Char] -> a
error [Char]
"unsupported block size in GF"
where gf :: Word64 -> Word64 -> (Word64, Word64)
gf :: Word64 -> Word64 -> (Word64, Word64)
gf Word64
srcLo Word64
srcHi =
((if Bool
carryLo then (forall a. Bits a => a -> a -> a
.|. Word64
1) else forall a. a -> a
id) (Word64
srcHi forall a. Bits a => a -> Int -> a
`shiftL` Int
1)
,(if Bool
carryHi then forall a. Bits a => a -> a -> a
xor Word64
0x87 else forall a. a -> a
id) forall a b. (a -> b) -> a -> b
$ (Word64
srcLo forall a. Bits a => a -> Int -> a
`shiftL` Int
1)
)
where carryHi :: Bool
carryHi = Word64
srcHi forall a. Bits a => a -> Int -> Bool
`testBit` Int
63
carryLo :: Bool
carryLo = Word64
srcLo forall a. Bits a => a -> Int -> Bool
`testBit` Int
63
len :: Int
len = forall ba. ByteArrayAccess ba => ba -> Int
B.length ba
b