1 {-# LANGUAGE DataKinds #-}
2 {-# LANGUAGE NoMonomorphismRestriction #-}
3 {-# LANGUAGE OverloadedStrings #-}
4 {-# LANGUAGE StrictData #-}
5 {-# LANGUAGE TypeApplications #-}
6 {-# OPTIONS_GHC -Wno-missing-signatures #-}
7 module Hjugement.CLI.Trustee where
9 import Control.Applicative (Applicative(..))
10 import Control.Monad (Monad(..))
11 import Control.Monad.Trans.Maybe (MaybeT(..))
12 import Control.Monad.Trans.State.Strict (runState)
14 import Data.Eq (Eq(..))
15 import Data.Foldable (null)
16 import Data.Function (($), (.), id, flip)
17 import Data.Functor ((<$>))
18 import Data.Maybe (Maybe(..))
19 import Data.Ord (Ord(..))
20 import Data.Proxy (Proxy(..))
21 import Data.Semigroup (Semigroup(..))
22 import GHC.Prim (coerce)
24 import Symantic.CLI as CLI
25 import Text.Show (Show(..))
26 import System.IO (FilePath)
27 import qualified Data.List as List
28 import qualified Data.Text as T
29 import qualified Pipes as Pip
30 import qualified Pipes.Prelude as Pip
31 import qualified Symantic.Document as Doc
32 import qualified System.FilePath as FP
33 import qualified System.Random as Rand
34 import qualified Voting.Protocol as VP
36 import Hjugement.CLI.Utils
39 data Trustee_Params = Trustee_Params
40 { trustee_crypto :: VP.FFC
41 , trustee_version :: VP.Version
45 "Commands for a trustee."
53 api_trustee_generate <!>
58 run_trustee globParams =
60 run_trustee_generate globParams params :!:
61 run_trustee_decrypt globParams params
66 api_trustee_generate =
67 "Run by a trustee to generate a share of an election key.\
68 \ Such a share consists of a private key and a public key with a certificate.\
69 \ Generated files are stored in the current directory with\
70 \ a name that starts with "<>fileRef "ID"<>",\
71 \ where "<>fileRef "ID"<>" is a short fingerprint of the public key.\
72 \ The private key is stored in "<>fileRef "ID.privkey"<>" and must be\
73 \ secured by the trustee. The public key is stored in "<>fileRef "ID.pubkey"<>" and must\
74 \ be sent to the election administrator."
79 glob@Global_Params{..}
81 VP.reify trustee_version $ \(_v::Proxy v) -> do
82 VP.reify trustee_crypto $ \(_c::Proxy c) -> do
83 (secKey, pubKey) <- Pip.liftIO $ Rand.getStdRandom $ runState $ do
84 secKey <- VP.randomSecretKey @VP.FFC @c
85 pubKey :: VP.TrusteePublicKey VP.FFC v c
86 <- VP.proveIndispensableTrusteePublicKey secKey
87 return (secKey, pubKey)
89 T.unpack $ T.toUpper $ T.take 8 $
90 VP.hexSHA256 $ VP.bytesNat $
91 VP.trustee_PublicKey pubKey
93 Pip.each [pubIdent] >-> pipeInfo glob (\ident ->
95 "generated trustee keypair "<>ident<>
96 " in "<>(global_dir FP.</> ident)<>".{privkey,pubkey}"
98 Pip.each [secKey] >-> writeJSON glob 0o400 (global_dir FP.</> pubIdent FP.<.>"privkey")
99 Pip.each [pubKey] >-> writeJSON glob 0o444 (global_dir FP.</> pubIdent FP.<.>"pubkey")
103 data TrusteeDecrypt_Params = TrusteeDecrypt_Params
104 { trusteeDecrypt_privkey :: FilePath
105 , trusteeDecrypt_url :: FilePath
108 api_trustee_decrypt =
109 "This command is run by each trustee to perform a partial decryption."
112 rule "TrusteeDecryptParams"
113 (TrusteeDecrypt_Params
114 <$> api_param_privkey
116 <?> response @(Maybe (VP.DecryptionShare VP.FFC VP.StableVersion ()))
119 "Read private key from file "<>ref"FILE"<>"."
121 requiredTag "privkey" (var "FILE")
124 glob@Global_Params{..}
126 TrusteeDecrypt_Params{..} =
127 VP.reify trustee_version $ \(_v::Proxy v) -> do
128 VP.reify trustee_crypto $ \(_crypto::Proxy c) -> runMaybeT $ do
129 (secKey::VP.E VP.FFC c) <- loadJSON glob trusteeDecrypt_privkey
130 let pubKey = VP.publicKey secKey
131 let trusteeKeysPath = trusteeDecrypt_url FP.</> "public_keys.jsons"
132 outputInfo glob "check that the public key is amongst the public keys of the election"
133 keys :: [VP.TrusteePublicKey VP.FFC v c] <- runPipeWithError glob $
135 readJSON glob trusteeKeysPath
136 >-> Pip.filter ((pubKey ==) . VP.trustee_PublicKey)
138 () | null keys -> outputError glob $
139 "the public key associated with the given secret key "<>
140 "is not within the list of public trustee keys of the election.\n"<>
142 [ "List of trustees' public keys: "<>Doc.from trusteeKeysPath
143 , "Trustee's public key: "<>Doc.from (show (VP.nat pubKey))
145 () | List.length keys > 1 -> outputError glob $
146 "the public key associated with the given secret key "<>
147 "appears more than one time in the list of public trustee keys of the election.\n"<>
149 [ "List of trustees' public keys: "<>Doc.from trusteeKeysPath
150 , "Trustee's public key: "<>Doc.from (show (VP.nat pubKey))
153 outputInfo glob "tally the encrypted ballots"
154 -- FIXME: actually support fetching through an URL
155 let ballotsPath = trusteeDecrypt_url FP.</> "ballots.jsons"
156 (encTally, _numBallots) <- runPipeWithError glob $
158 (flip VP.insertEncryptedTally)
159 VP.emptyEncryptedTally id $
160 readJSON glob ballotsPath
161 decShare :: VP.DecryptionShare VP.FFC v c
163 Rand.getStdRandom $ runState $
164 VP.proveDecryptionShare encTally secKey
165 return (coerce decShare :: VP.DecryptionShare VP.FFC VP.StableVersion ())