replace the Lioness key derivation by a less a hacky one

This commit is contained in:
Balazs Komuves 2026-04-30 10:50:45 +02:00
parent e1c85e4e24
commit 8addd5efff
No known key found for this signature in database
GPG Key ID: F63B7AEF18435562
2 changed files with 14 additions and 12 deletions

View File

@ -24,7 +24,7 @@ import Octet
type ByteStream = [Word8]
type MasterKey = Key256
type KeyDerivFun256 = Key256 -> ByteStream
type KeyDerivFun256 = Domain -> Key256 -> Word256
type KeyedHashFun256 = Key256 -> [Word8] -> Word256
type StreamGen256 = Key256 -> ByteStream
@ -44,11 +44,10 @@ type LionessKeys = (Key256,Key256,Key256,Key256)
lionessDeriveKeys :: LionessInstance -> MasterKey -> LionessKeys
lionessDeriveKeys (MkLioness kdfFun _ _) masterKey = (k1,k2,k3,k4) where
[k1,k2,k3,k4] = map (Key256 . W256) $ partition 32 $ take 128 $ kdfFun masterKey
partition :: Int -> [a] -> [[a]]
partition m [] = []
partition m xs = take m xs : partition m (drop m xs)
k1 = Key256 (kdfFun LionessKey1 masterKey)
k2 = Key256 (kdfFun LionessKey2 masterKey)
k3 = Key256 (kdfFun LionessKey3 masterKey)
k4 = Key256 (kdfFun LionessKey4 masterKey)
--------------------------------------------------------------------------------
@ -109,13 +108,8 @@ lionessInvPerm inst@(MkLioness kdfFun hashFun streamFun) masterKey input
--------------------------------------------------------------------------------
twistIV :: IV -> IV
twistIV (IV orig) = IV (orig `xor128` twist) where
twist = wordFromInteger 0x1234567890abcdef_aa55aa55aa55aa55
testKdfFun :: KeyDerivFun256
testKdfFun bigKey = case splitKey256 bigKey of
(key,iv) -> streamCipherPRGBytes AES128_CTR key (twistIV iv)
testKdfFun domain (Key256 masterKey) = kdf256 KDF_SHA256 domain (fromWord256 masterKey)
testHashFun :: KeyedHashFun256
testHashFun (Key256 bigKey) input = hash SHA256 (fromWord256 bigKey ++ input)

View File

@ -50,6 +50,10 @@ data Domain
| SphinxMacKey -- ^ key for the MAC in the Sphinx header
| SphinxPayloadEncKey -- ^ key to encrypt the Sphinx payload
| SphinxBlinding -- ^ key to compute the blinding factor
| LionessKey1 -- ^ K1 of Lioness
| LionessKey2 -- ^ K2 of Lioness
| LionessKey3 -- ^ K3 of Lioness
| LionessKey4 -- ^ K4 of Lioness
deriving (Eq,Show)
-- | Key derivation functions
@ -75,6 +79,10 @@ domainConstant domain =
SphinxMacKey -> asciiStringToWord128 "mac-key"
SphinxPayloadEncKey -> asciiStringToWord128 "payload-enc-key"
SphinxBlinding -> asciiStringToWord128 "sphinx-blinding"
LionessKey1 -> asciiStringToWord128 "lioness-key1"
LionessKey2 -> asciiStringToWord128 "lioness-key2"
LionessKey3 -> asciiStringToWord128 "lioness-key3"
LionessKey4 -> asciiStringToWord128 "lioness-key4"
where
asciiStringToWord128 :: String -> Word128
asciiStringToWord128 input