[AES] repair genCounter

This commit is contained in:
Vincent Hanquez 2015-04-15 11:25:44 +01:00
parent cd216f5ee9
commit dc1215dbd7

View File

@ -46,13 +46,10 @@ module Crypto.Cipher.AES.Primitive
import Data.Word import Data.Word
import Foreign.Ptr import Foreign.Ptr
import Foreign.ForeignPtr
import Foreign.C.Types import Foreign.C.Types
import Foreign.C.String import Foreign.C.String
import Data.ByteString.Internal import Data.ByteString.Internal
import Data.ByteString.Unsafe
import qualified Data.ByteString as B import qualified Data.ByteString as B
import qualified Data.ByteString.Internal as B (ByteString(PS), mallocByteString, memcpy)
import System.IO.Unsafe (unsafePerformIO) import System.IO.Unsafe (unsafePerformIO)
import Crypto.Error import Crypto.Error
@ -105,14 +102,12 @@ keyToPtr (AES b) f = withByteArray b (f . castPtr)
ivToPtr :: ByteArrayAccess iv => iv -> (Ptr Word8 -> IO a) -> IO a ivToPtr :: ByteArrayAccess iv => iv -> (Ptr Word8 -> IO a) -> IO a
ivToPtr iv f = withByteArray iv (f . castPtr) ivToPtr iv f = withByteArray iv (f . castPtr)
{-
ivCopyPtr :: IV AES -> (Ptr Word8 -> IO ()) -> IO (IV AES) ivCopyPtr :: IV AES -> (Ptr Word8 -> IO a) -> IO (a, IV AES)
ivCopyPtr (IV iv) f = do ivCopyPtr (IV iv) f = (\(x,y) -> (x, IV y)) `fmap` copyAndModify iv f
newIV <- byteArrayAlloc 16 $ \newPtr -> do where
withByteArray iv $ \ivPtr -> B.memcpy newPtr ivPtr 16 copyAndModify :: ByteArray ba => ba -> (Ptr Word8 -> IO a) -> IO (a, ba)
withByteArray newIV $ f copyAndModify ba f' = byteArrayCopyRet ba f'
return $! IV newIV
-}
withKeyAndIV :: ByteArrayAccess iv => AES -> iv -> (Ptr AES -> Ptr Word8 -> IO a) -> IO a withKeyAndIV :: ByteArrayAccess iv => AES -> iv -> (Ptr AES -> Ptr Word8 -> IO a) -> IO a
withKeyAndIV ctx iv f = keyToPtr ctx $ \kptr -> ivToPtr iv $ \ivp -> f kptr ivp withKeyAndIV ctx iv f = keyToPtr ctx $ \kptr -> ivToPtr iv $ \ivp -> f kptr ivp
@ -194,27 +189,22 @@ genCTR ctx (IV iv) len
-- --
-- Similiar to 'genCTR' but also return the next IV for continuation -- Similiar to 'genCTR' but also return the next IV for continuation
{-# NOINLINE genCounter #-} {-# NOINLINE genCounter #-}
genCounter :: AES genCounter :: ByteArray ba
=> AES
-> IV AES -> IV AES
-> Int -> Int
-> (ByteString, IV AES) -> (ba, IV AES)
genCounter ctx iv len genCounter ctx iv len
| len <= 0 = (B.empty, iv) | len <= 0 = (empty, iv)
| otherwise = unsafePerformIO $ do | otherwise = unsafePerformIO $
undefined keyToPtr ctx $ \k ->
{- ivCopyPtr iv $ \i ->
fptr <- B.mallocByteString outputLength byteArrayAlloc outputLength $ \o -> do
newIv <- withForeignPtr fptr $ \o -> c_aes_gen_ctr_cont (castPtr o) k i (fromIntegral nbBlocks)
keyToPtr ctx $ \k ->
ivCopyPtr iv $ \i -> do
c_aes_gen_ctr_cont (castPtr o) k i (fromIntegral nbBlocks)
let !out = B.PS fptr 0 outputLength
return $! (out `seq` newIv `seq` (out, newIv))
where where
(nbBlocks',r) = len `quotRem` 16 (nbBlocks',r) = len `quotRem` 16
nbBlocks = if r == 0 then nbBlocks' else nbBlocks' + 1 nbBlocks = if r == 0 then nbBlocks' else nbBlocks' + 1
outputLength = nbBlocks * 16 outputLength = nbBlocks * 16
-}
{- TODO: when genCTR has same AESIV requirements for IV, add the following rules: {- TODO: when genCTR has same AESIV requirements for IV, add the following rules:
- RULES "snd . genCounter" forall ctx iv len . snd (genCounter ctx iv len) = genCTR ctx iv len - RULES "snd . genCounter" forall ctx iv len . snd (genCounter ctx iv len) = genCTR ctx iv len