{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Ouroboros.Consensus.Protocol.Ledger.HotKey (
KESEvolution
, KESInfo (..)
, kesAbsolutePeriod
, KESStatus (..)
, kesStatus
, HotKey (..)
, KESEvolutionError (..)
, KESEvolutionInfo
, mkHotKey
, sign
) where
import qualified Cardano.Crypto.KES as Relative (Period)
import Cardano.Ledger.Crypto (Crypto)
import qualified Cardano.Ledger.Keys as SL
import qualified Cardano.Protocol.TPraos.OCert as Absolute (KESPeriod (..))
import Data.Word (Word64)
import GHC.Generics (Generic)
import GHC.Stack (HasCallStack)
import Ouroboros.Consensus.Block.Forging (UpdateInfo (..))
import Ouroboros.Consensus.Util.IOLike
type KESEvolution = Relative.Period
data KESInfo = KESInfo {
KESInfo -> KESPeriod
kesStartPeriod :: !Absolute.KESPeriod
, KESInfo -> KESPeriod
kesEndPeriod :: !Absolute.KESPeriod
, KESInfo -> Word
kesEvolution :: !KESEvolution
}
deriving (Int -> KESInfo -> ShowS
[KESInfo] -> ShowS
KESInfo -> String
(Int -> KESInfo -> ShowS)
-> (KESInfo -> String) -> ([KESInfo] -> ShowS) -> Show KESInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> KESInfo -> ShowS
showsPrec :: Int -> KESInfo -> ShowS
$cshow :: KESInfo -> String
show :: KESInfo -> String
$cshowList :: [KESInfo] -> ShowS
showList :: [KESInfo] -> ShowS
Show, (forall x. KESInfo -> Rep KESInfo x)
-> (forall x. Rep KESInfo x -> KESInfo) -> Generic KESInfo
forall x. Rep KESInfo x -> KESInfo
forall x. KESInfo -> Rep KESInfo x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. KESInfo -> Rep KESInfo x
from :: forall x. KESInfo -> Rep KESInfo x
$cto :: forall x. Rep KESInfo x -> KESInfo
to :: forall x. Rep KESInfo x -> KESInfo
Generic, Context -> KESInfo -> IO (Maybe ThunkInfo)
Proxy KESInfo -> String
(Context -> KESInfo -> IO (Maybe ThunkInfo))
-> (Context -> KESInfo -> IO (Maybe ThunkInfo))
-> (Proxy KESInfo -> String)
-> NoThunks KESInfo
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
$cnoThunks :: Context -> KESInfo -> IO (Maybe ThunkInfo)
noThunks :: Context -> KESInfo -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> KESInfo -> IO (Maybe ThunkInfo)
wNoThunks :: Context -> KESInfo -> IO (Maybe ThunkInfo)
$cshowTypeOf :: Proxy KESInfo -> String
showTypeOf :: Proxy KESInfo -> String
NoThunks)
kesAbsolutePeriod :: KESInfo -> Absolute.KESPeriod
kesAbsolutePeriod :: KESInfo -> KESPeriod
kesAbsolutePeriod KESInfo { KESPeriod
kesStartPeriod :: KESInfo -> KESPeriod
kesStartPeriod :: KESPeriod
kesStartPeriod, Word
kesEvolution :: KESInfo -> Word
kesEvolution :: Word
kesEvolution } =
Word -> KESPeriod
Absolute.KESPeriod (Word -> KESPeriod) -> Word -> KESPeriod
forall a b. (a -> b) -> a -> b
$ Word
start Word -> Word -> Word
forall a. Num a => a -> a -> a
+ Word
kesEvolution
where
Absolute.KESPeriod Word
start = KESPeriod
kesStartPeriod
data KESStatus =
BeforeKESStart
Absolute.KESPeriod
Absolute.KESPeriod
| InKESRange
KESEvolution
| AfterKESEnd
Absolute.KESPeriod
Absolute.KESPeriod
kesStatus :: KESInfo -> Absolute.KESPeriod -> KESStatus
kesStatus :: KESInfo -> KESPeriod -> KESStatus
kesStatus KESInfo { kesStartPeriod :: KESInfo -> KESPeriod
kesStartPeriod = lo' :: KESPeriod
lo'@(Absolute.KESPeriod Word
lo)
, kesEndPeriod :: KESInfo -> KESPeriod
kesEndPeriod = hi' :: KESPeriod
hi'@(Absolute.KESPeriod Word
hi)
}
cur' :: KESPeriod
cur'@(Absolute.KESPeriod Word
cur)
| Word
cur Word -> Word -> Bool
forall a. Ord a => a -> a -> Bool
< Word
lo = KESPeriod -> KESPeriod -> KESStatus
BeforeKESStart KESPeriod
cur' KESPeriod
lo'
| Word
cur Word -> Word -> Bool
forall a. Ord a => a -> a -> Bool
>= Word
hi = KESPeriod -> KESPeriod -> KESStatus
AfterKESEnd KESPeriod
cur' KESPeriod
hi'
| Bool
otherwise = Word -> KESStatus
InKESRange (Word
cur Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
lo)
data KESEvolutionError =
KESCouldNotEvolve
KESInfo
Absolute.KESPeriod
| KESKeyAlreadyPoisoned
KESInfo
Absolute.KESPeriod
deriving (Int -> KESEvolutionError -> ShowS
[KESEvolutionError] -> ShowS
KESEvolutionError -> String
(Int -> KESEvolutionError -> ShowS)
-> (KESEvolutionError -> String)
-> ([KESEvolutionError] -> ShowS)
-> Show KESEvolutionError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> KESEvolutionError -> ShowS
showsPrec :: Int -> KESEvolutionError -> ShowS
$cshow :: KESEvolutionError -> String
show :: KESEvolutionError -> String
$cshowList :: [KESEvolutionError] -> ShowS
showList :: [KESEvolutionError] -> ShowS
Show)
type KESEvolutionInfo = UpdateInfo KESInfo KESEvolutionError
data HotKey c m = HotKey {
forall c (m :: * -> *).
HotKey c m -> KESPeriod -> m KESEvolutionInfo
evolve :: Absolute.KESPeriod -> m KESEvolutionInfo
, forall c (m :: * -> *). HotKey c m -> m KESInfo
getInfo :: m KESInfo
, forall c (m :: * -> *). HotKey c m -> m Bool
isPoisoned :: m Bool
, forall c (m :: * -> *).
HotKey c m
-> forall toSign.
(KESignable c toSign, HasCallStack) =>
toSign -> m (SignedKES c toSign)
sign_ :: forall toSign. (SL.KESignable c toSign, HasCallStack)
=> toSign -> m (SL.SignedKES c toSign)
}
sign ::
(SL.KESignable c toSign, HasCallStack)
=> HotKey c m
-> toSign -> m (SL.SignedKES c toSign)
sign :: forall c toSign (m :: * -> *).
(KESignable c toSign, HasCallStack) =>
HotKey c m -> toSign -> m (SignedKES c toSign)
sign = HotKey c m -> toSign -> m (SignedKES (KES c) toSign)
HotKey c m
-> forall toSign.
(KESignable c toSign, HasCallStack) =>
toSign -> m (SignedKES c toSign)
forall c (m :: * -> *).
HotKey c m
-> forall toSign.
(KESignable c toSign, HasCallStack) =>
toSign -> m (SignedKES c toSign)
sign_
data KESKey c =
KESKey !(SL.SignKeyKES c)
| KESKeyPoisoned
deriving ((forall x. KESKey c -> Rep (KESKey c) x)
-> (forall x. Rep (KESKey c) x -> KESKey c) -> Generic (KESKey c)
forall x. Rep (KESKey c) x -> KESKey c
forall x. KESKey c -> Rep (KESKey c) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall c x. Rep (KESKey c) x -> KESKey c
forall c x. KESKey c -> Rep (KESKey c) x
$cfrom :: forall c x. KESKey c -> Rep (KESKey c) x
from :: forall x. KESKey c -> Rep (KESKey c) x
$cto :: forall c x. Rep (KESKey c) x -> KESKey c
to :: forall x. Rep (KESKey c) x -> KESKey c
Generic)
instance Crypto c => NoThunks (KESKey c)
kesKeyIsPoisoned :: KESKey c -> Bool
kesKeyIsPoisoned :: forall c. KESKey c -> Bool
kesKeyIsPoisoned KESKey c
KESKeyPoisoned = Bool
True
kesKeyIsPoisoned (KESKey SignKeyKES c
_) = Bool
False
data KESState c = KESState {
forall c. KESState c -> KESInfo
kesStateInfo :: !KESInfo
, forall c. KESState c -> KESKey c
kesStateKey :: !(KESKey c)
}
deriving ((forall x. KESState c -> Rep (KESState c) x)
-> (forall x. Rep (KESState c) x -> KESState c)
-> Generic (KESState c)
forall x. Rep (KESState c) x -> KESState c
forall x. KESState c -> Rep (KESState c) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall c x. Rep (KESState c) x -> KESState c
forall c x. KESState c -> Rep (KESState c) x
$cfrom :: forall c x. KESState c -> Rep (KESState c) x
from :: forall x. KESState c -> Rep (KESState c) x
$cto :: forall c x. Rep (KESState c) x -> KESState c
to :: forall x. Rep (KESState c) x -> KESState c
Generic)
instance Crypto c => NoThunks (KESState c)
mkHotKey ::
forall m c. (Crypto c, IOLike m)
=> SL.SignKeyKES c
-> Absolute.KESPeriod
-> Word64
-> m (HotKey c m)
mkHotKey :: forall (m :: * -> *) c.
(Crypto c, IOLike m) =>
SignKeyKES c -> KESPeriod -> Word64 -> m (HotKey c m)
mkHotKey SignKeyKES (KES c)
initKey startPeriod :: KESPeriod
startPeriod@(Absolute.KESPeriod Word
start) Word64
maxKESEvolutions = do
StrictMVar m (KESState c)
varKESState <- KESState c -> m (StrictMVar m (KESState c))
forall (m :: * -> *) a.
(HasCallStack, MonadMVar m, NoThunks a) =>
a -> m (StrictMVar m a)
newMVar KESState c
initKESState
HotKey c m -> m (HotKey c m)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return HotKey {
evolve :: KESPeriod -> m KESEvolutionInfo
evolve = StrictMVar m (KESState c) -> KESPeriod -> m KESEvolutionInfo
forall (m :: * -> *) c.
(Crypto c, IOLike m) =>
StrictMVar m (KESState c) -> KESPeriod -> m KESEvolutionInfo
evolveKey StrictMVar m (KESState c)
varKESState
, getInfo :: m KESInfo
getInfo = KESState c -> KESInfo
forall c. KESState c -> KESInfo
kesStateInfo (KESState c -> KESInfo) -> m (KESState c) -> m KESInfo
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> StrictMVar m (KESState c) -> m (KESState c)
forall (m :: * -> *) a. MonadMVar m => StrictMVar m a -> m a
readMVar StrictMVar m (KESState c)
varKESState
, isPoisoned :: m Bool
isPoisoned = KESKey c -> Bool
forall c. KESKey c -> Bool
kesKeyIsPoisoned (KESKey c -> Bool)
-> (KESState c -> KESKey c) -> KESState c -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KESState c -> KESKey c
forall c. KESState c -> KESKey c
kesStateKey (KESState c -> Bool) -> m (KESState c) -> m Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> StrictMVar m (KESState c) -> m (KESState c)
forall (m :: * -> *) a. MonadMVar m => StrictMVar m a -> m a
readMVar StrictMVar m (KESState c)
varKESState
, sign_ :: forall toSign.
(KESignable c toSign, HasCallStack) =>
toSign -> m (SignedKES c toSign)
sign_ = \toSign
toSign -> do
KESState { KESInfo
kesStateInfo :: forall c. KESState c -> KESInfo
kesStateInfo :: KESInfo
kesStateInfo, KESKey c
kesStateKey :: forall c. KESState c -> KESKey c
kesStateKey :: KESKey c
kesStateKey } <- StrictMVar m (KESState c) -> m (KESState c)
forall (m :: * -> *) a. MonadMVar m => StrictMVar m a -> m a
readMVar StrictMVar m (KESState c)
varKESState
case KESKey c
kesStateKey of
KESKey c
KESKeyPoisoned -> String -> m (SignedKES c toSign)
forall a. HasCallStack => String -> a
error String
"trying to sign with a poisoned key"
KESKey SignKeyKES (KES c)
key -> do
let evolution :: Word
evolution = KESInfo -> Word
kesEvolution KESInfo
kesStateInfo
signed :: SignedKES c toSign
signed = ContextKES (KES c)
-> Word -> toSign -> SignKeyKES (KES c) -> SignedKES c toSign
forall v a.
(KESAlgorithm v, Signable v a) =>
ContextKES v -> Word -> a -> SignKeyKES v -> SignedKES v a
SL.signedKES () Word
evolution toSign
toSign SignKeyKES (KES c)
key
SignedKES c toSign -> m (SignedKES c toSign)
forall a. a -> m a
forall (m :: * -> *) a. MonadEvaluate m => a -> m a
evaluate SignedKES c toSign
signed
}
where
initKESState :: KESState c
initKESState :: KESState c
initKESState = KESState {
kesStateInfo :: KESInfo
kesStateInfo = KESInfo {
kesStartPeriod :: KESPeriod
kesStartPeriod = KESPeriod
startPeriod
, kesEndPeriod :: KESPeriod
kesEndPeriod = Word -> KESPeriod
Absolute.KESPeriod (Word
start Word -> Word -> Word
forall a. Num a => a -> a -> a
+ Word64 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
maxKESEvolutions)
, kesEvolution :: Word
kesEvolution = Word
0
}
, kesStateKey :: KESKey c
kesStateKey = SignKeyKES (KES c) -> KESKey c
forall c. SignKeyKES c -> KESKey c
KESKey SignKeyKES (KES c)
initKey
}
evolveKey ::
forall m c. (Crypto c, IOLike m)
=> StrictMVar m (KESState c) -> Absolute.KESPeriod -> m KESEvolutionInfo
evolveKey :: forall (m :: * -> *) c.
(Crypto c, IOLike m) =>
StrictMVar m (KESState c) -> KESPeriod -> m KESEvolutionInfo
evolveKey StrictMVar m (KESState c)
varKESState KESPeriod
targetPeriod = StrictMVar m (KESState c)
-> (KESState c -> m (KESState c, KESEvolutionInfo))
-> m KESEvolutionInfo
forall (m :: * -> *) a b.
(HasCallStack, MonadMVar m) =>
StrictMVar m a -> (a -> m (a, b)) -> m b
modifyMVar StrictMVar m (KESState c)
varKESState ((KESState c -> m (KESState c, KESEvolutionInfo))
-> m KESEvolutionInfo)
-> (KESState c -> m (KESState c, KESEvolutionInfo))
-> m KESEvolutionInfo
forall a b. (a -> b) -> a -> b
$ \KESState c
kesState -> do
let info :: KESInfo
info = KESState c -> KESInfo
forall c. KESState c -> KESInfo
kesStateInfo KESState c
kesState
m (KESState c, KESEvolutionInfo)
-> m (KESState c, KESEvolutionInfo)
forall a. m a -> m a
forall (m :: * -> *) a. MonadMask m => m a -> m a
uninterruptibleMask_ (m (KESState c, KESEvolutionInfo)
-> m (KESState c, KESEvolutionInfo))
-> m (KESState c, KESEvolutionInfo)
-> m (KESState c, KESEvolutionInfo)
forall a b. (a -> b) -> a -> b
$ case KESState c -> KESKey c
forall c. KESState c -> KESKey c
kesStateKey KESState c
kesState of
KESKey c
KESKeyPoisoned ->
let err :: KESEvolutionError
err = KESInfo -> KESPeriod -> KESEvolutionError
KESKeyAlreadyPoisoned KESInfo
info KESPeriod
targetPeriod
in (KESState c, KESEvolutionInfo) -> m (KESState c, KESEvolutionInfo)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (KESState c
kesState, KESEvolutionError -> KESEvolutionInfo
forall updated failed. failed -> UpdateInfo updated failed
UpdateFailed KESEvolutionError
err)
KESKey SignKeyKES c
key -> case KESInfo -> KESPeriod -> KESStatus
kesStatus KESInfo
info KESPeriod
targetPeriod of
BeforeKESStart {} ->
(KESState c, KESEvolutionInfo) -> m (KESState c, KESEvolutionInfo)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (KESState c
kesState, KESInfo -> KESEvolutionInfo
forall updated failed. updated -> UpdateInfo updated failed
Updated KESInfo
info)
AfterKESEnd {} ->
let err :: KESEvolutionError
err = KESInfo -> KESPeriod -> KESEvolutionError
KESCouldNotEvolve KESInfo
info KESPeriod
targetPeriod
in (KESState c, KESEvolutionInfo) -> m (KESState c, KESEvolutionInfo)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (KESState c -> KESState c
poisonState KESState c
kesState, KESEvolutionError -> KESEvolutionInfo
forall updated failed. failed -> UpdateInfo updated failed
UpdateFailed KESEvolutionError
err)
InKESRange Word
targetEvolution
| Word
targetEvolution Word -> Word -> Bool
forall a. Ord a => a -> a -> Bool
<= KESInfo -> Word
kesEvolution KESInfo
info
-> (KESState c, KESEvolutionInfo) -> m (KESState c, KESEvolutionInfo)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (KESState c
kesState, KESInfo -> KESEvolutionInfo
forall updated failed. updated -> UpdateInfo updated failed
Updated KESInfo
info)
| Bool
otherwise
-> (\KESState c
s' -> (KESState c
s', KESInfo -> KESEvolutionInfo
forall updated failed. updated -> UpdateInfo updated failed
Updated (KESState c -> KESInfo
forall c. KESState c -> KESInfo
kesStateInfo KESState c
s'))) (KESState c -> (KESState c, KESEvolutionInfo))
-> m (KESState c) -> m (KESState c, KESEvolutionInfo)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
Word -> KESInfo -> SignKeyKES c -> m (KESState c)
go Word
targetEvolution KESInfo
info SignKeyKES c
key
where
poisonState :: KESState c -> KESState c
poisonState :: KESState c -> KESState c
poisonState KESState c
kesState = KESState c
kesState { kesStateKey = KESKeyPoisoned }
go :: KESEvolution -> KESInfo -> SL.SignKeyKES c -> m (KESState c)
go :: Word -> KESInfo -> SignKeyKES c -> m (KESState c)
go Word
targetEvolution KESInfo
info SignKeyKES c
key
| Word
targetEvolution Word -> Word -> Bool
forall a. Ord a => a -> a -> Bool
<= Word
curEvolution
= KESState c -> m (KESState c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (KESState c -> m (KESState c)) -> KESState c -> m (KESState c)
forall a b. (a -> b) -> a -> b
$ KESState { kesStateInfo :: KESInfo
kesStateInfo = KESInfo
info, kesStateKey :: KESKey c
kesStateKey = SignKeyKES c -> KESKey c
forall c. SignKeyKES c -> KESKey c
KESKey SignKeyKES c
key }
| Bool
otherwise
= case ContextKES (KES c) -> SignKeyKES c -> Word -> Maybe (SignKeyKES c)
forall v.
(KESAlgorithm v, HasCallStack) =>
ContextKES v -> SignKeyKES v -> Word -> Maybe (SignKeyKES v)
SL.updateKES () SignKeyKES c
key Word
curEvolution of
Maybe (SignKeyKES c)
Nothing -> String -> m (KESState c)
forall a. HasCallStack => String -> a
error String
"Could not update KES key"
Just !SignKeyKES c
key' -> do
SignKeyKES c -> m ()
forall v. KESAlgorithm v => SignKeyKES v -> m ()
forall (m :: * -> *) v.
(IOLike m, KESAlgorithm v) =>
SignKeyKES v -> m ()
forgetSignKeyKES SignKeyKES c
key
let info' :: KESInfo
info' = KESInfo
info { kesEvolution = curEvolution + 1 }
Word -> KESInfo -> SignKeyKES c -> m (KESState c)
go Word
targetEvolution KESInfo
info' SignKeyKES c
key'
where
curEvolution :: Word
curEvolution = KESInfo -> Word
kesEvolution KESInfo
info