-- | Skein as a key derivation function.
module Crypto.Threefish.Skein.KDF (deriveKey, deriveKeys) where
import Crypto.Threefish.Skein.Internal
import Crypto.Threefish.Skein
import Crypto.Threefish.UBI
import Crypto.Threefish.Threefish256
import Data.Serialize
import qualified Data.ByteString as BS
import Data.ByteString.Unsafe
import System.IO.Unsafe
import Foreign.Marshal.Alloc
import Foreign.Ptr

-- | Derive up to 2^64 keys from a master key.
--   The key identifiers will be 0, 1, ... 2^64-1.
deriveKeys :: Key256 -> [Key256]
deriveKeys :: Key256 -> [Key256]
deriveKeys Key256
mk =
  [Key256 -> Key256 -> Key256
deriveKey Key256
mk (ByteString -> Key256
Block256 (ByteString -> Key256) -> ByteString -> Key256
forall a b. (a -> b) -> a -> b
$ Put -> ByteString
runPut (Put -> ByteString) -> Put -> ByteString
forall a b. (a -> b) -> a -> b
$ (Word64 -> Put) -> [Word64] -> Put
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Word64 -> Put
putWord64le [Word64
kid,Word64
0,Word64
0,Word64
0]) |
   Word64
kid <- [Word64
0..]]

-- | Derive a key from a master key using a custom key identifier.
deriveKey :: Key256 -> Block256 -> Key256
deriveKey :: Key256 -> Key256 -> Key256
deriveKey (Block256 ByteString
mk) (Block256 ByteString
kid) =
    IO Key256 -> Key256
forall a. IO a -> a
unsafePerformIO (IO Key256 -> Key256) -> IO Key256 -> Key256
forall a b. (a -> b) -> a -> b
$ do
      Int -> (Ptr Word64 -> IO Key256) -> IO Key256
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes Int
64 ((Ptr Word64 -> IO Key256) -> IO Key256)
-> (Ptr Word64 -> IO Key256) -> IO Key256
forall a b. (a -> b) -> a -> b
$ \Ptr Word64
ctx -> do
        Int -> (Ptr Word64 -> IO Key256) -> IO Key256
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes Int
32 ((Ptr Word64 -> IO Key256) -> IO Key256)
-> (Ptr Word64 -> IO Key256) -> IO Key256
forall a b. (a -> b) -> a -> b
$ \Ptr Word64
outkey -> do
          ByteString -> (CString -> IO Key256) -> IO Key256
forall a. ByteString -> (CString -> IO a) -> IO a
unsafeUseAsCString ByteString
mk ((CString -> IO Key256) -> IO Key256)
-> (CString -> IO Key256) -> IO Key256
forall a b. (a -> b) -> a -> b
$ \CString
masterkey -> do
            ByteString -> (CString -> IO Key256) -> IO Key256
forall a. ByteString -> (CString -> IO a) -> IO a
unsafeUseAsCString ByteString
kid ((CString -> IO Key256) -> IO Key256)
-> (CString -> IO Key256) -> IO Key256
forall a b. (a -> b) -> a -> b
$ \CString
keyid -> do
              Ptr Word64 -> Ptr Word64 -> Word64 -> IO ()
skein256_init Ptr Word64
ctx (CString -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr CString
masterkey) Word64
256
              Ptr Word64 -> Int -> Int -> Word64 -> Ptr Word64 -> IO ()
skein256_update Ptr Word64
ctx Int
3 (BlockType -> Int
type2int BlockType
KeyIdentifier) Word64
l (CString -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr CString
keyid)
              Ptr Word64 -> Int -> Int -> Ptr Word64 -> IO ()
skein256_output Ptr Word64
ctx Int
0 Int
0 Ptr Word64
outkey
              ByteString -> Key256
Block256 (ByteString -> Key256) -> IO ByteString -> IO Key256
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` CStringLen -> IO ByteString
BS.packCStringLen (Ptr Word64 -> CString
forall a b. Ptr a -> Ptr b
castPtr Ptr Word64
outkey, Int
32)
  where
    l :: Word64
l = Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word64) -> Int -> Word64
forall a b. (a -> b) -> a -> b
$ ByteString -> Int
BS.length ByteString
kid