1 {-# LANGUAGE DeriveAnyClass #-}
2 {-# LANGUAGE DeriveGeneric #-}
3 {-# LANGUAGE DerivingStrategies #-}
4 module Voting.Protocol.Credential where
6 import Control.DeepSeq (NFData)
7 import Control.Monad (Monad(..), forM_, replicateM)
9 import Data.Char (Char)
10 import Data.Either (Either(..), either)
11 import Data.Eq (Eq(..))
12 import Data.Function (($))
13 import Data.Functor ((<$>))
15 import Data.Maybe (maybe)
16 import Data.Ord (Ord(..))
17 import Data.Semigroup (Semigroup(..))
18 import Data.Text (Text)
19 import GHC.Generics (Generic)
20 import Prelude (Integral(..), fromIntegral)
21 import Text.Show (Show(..))
22 import qualified Control.Monad.Trans.State.Strict as S
23 import qualified Crypto.KDF.PBKDF2 as Crypto
24 import qualified Data.Aeson as JSON
25 import qualified Data.Aeson.Types as JSON
26 import qualified Data.Char as Char
27 import qualified Data.List as List
28 import qualified Data.Text as Text
29 import qualified Data.Text.Encoding as Text
30 import qualified System.Random as Random
32 import Voting.Protocol.FFC
34 -- * Type 'Credential'
35 -- | A 'Credential' is a word of @('tokenLength'+1 '==' 15)@-characters
36 -- from a base alphabet of (@'tokenBase' '==' 58)@ characters:
37 -- "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
38 -- (beware the absence of "0", \"O", \"I", and "l").
39 -- The last character is a checksum.
40 -- The entropy is: @('tokenLength' * log 'tokenBase' / log 2) '==' 82.01… bits@.
41 newtype Credential = Credential Text
42 deriving (Eq,Show,Generic)
43 deriving newtype NFData
44 deriving newtype JSON.ToJSON
45 instance JSON.FromJSON Credential where
46 parseJSON json@(JSON.String s) =
47 either (\err -> JSON.typeMismatch ("Credential: "<>show err) json) return $
49 parseJSON json = JSON.typeMismatch "Credential" json
51 credentialAlphabet :: [Char] -- TODO: make this an array
52 credentialAlphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
54 tokenBase = List.length credentialAlphabet
58 -- | @'randomCredential'@ generates a random 'Credential'.
59 randomCredential :: Monad m => Random.RandomGen r => S.StateT r m Credential
61 rs <- replicateM tokenLength (randomR (fromIntegral tokenBase))
62 let (tot, cs) = List.foldl' (\(acc,ds) d ->
64 , charOfDigit d : ds )
66 let checksum = (neg tot + 53) `mod` 53 -- NOTE: why 53 and not 'tokenBase' ?
67 return $ Credential $ Text.reverse $ Text.pack (charOfDigit checksum:cs)
69 charOfDigit = (credentialAlphabet List.!!)
71 -- | @'readCredential'@ reads and check the well-formedness of a 'Credential'
73 readCredential :: Text -> Either ErrorToken Credential
75 | Text.length s /= tokenLength + 1 = Left ErrorToken_Length
78 (\acc c -> acc >>= \a -> ((a * tokenBase) +) <$> digitOfChar c)
81 checksum <- digitOfChar (Text.last s)
82 if (tot + checksum) `mod` 53 == 0
83 then Right (Credential s)
84 else Left ErrorToken_Checksum
87 maybe (Left $ ErrorToken_BadChar c) Right $
88 List.elemIndex c credentialAlphabet
90 -- ** Type 'ErrorToken'
92 = ErrorToken_BadChar Char.Char
95 deriving (Eq,Show,Generic,NFData)
98 newtype UUID = UUID Text
99 deriving (Eq,Ord,Show,Generic)
100 deriving anyclass (JSON.ToJSON)
101 deriving newtype NFData
102 instance JSON.FromJSON UUID where
103 parseJSON json@(JSON.String s) =
104 either (\err -> JSON.typeMismatch ("UUID: "<>show err) json) return $
106 parseJSON json = JSON.typeMismatch "UUID" json
108 -- | @'randomUUID'@ generates a random 'UUID'.
111 Random.RandomGen r =>
114 rs <- replicateM tokenLength (randomR (fromIntegral tokenBase))
115 return $ UUID $ Text.pack $ charOfDigit <$> rs
117 charOfDigit = (credentialAlphabet List.!!)
119 -- | @'readCredential'@ reads and check the well-formedness of a 'Credential'
121 readUUID :: Text -> Either ErrorToken UUID
123 | Text.length s /= tokenLength = Left ErrorToken_Length
125 forM_ (Text.unpack s) digitOfChar
129 maybe (Left $ ErrorToken_BadChar c) Right $
130 List.elemIndex c credentialAlphabet
132 -- ** Type 'SecretKey'
135 randomSecretKey :: Reifies c FFC => Monad m => RandomGen r => S.StateT r m (SecretKey c)
136 randomSecretKey = random
138 -- | @('credentialSecretKey' uuid cred)@ returns the 'SecretKey'
139 -- derived from given 'uuid' and 'cred'
140 -- using 'Crypto.fastPBKDF2_SHA256'.
141 credentialSecretKey :: Reifies c FFC => UUID -> Credential -> (SecretKey c)
142 credentialSecretKey (UUID uuid) (Credential cred) =
143 fromNatural $ decodeBigEndian $
144 Crypto.fastPBKDF2_SHA256
146 { Crypto.iterCounts = 1000
147 , Crypto.outputLength = 32 -- bytes, ie. 256 bits
149 (Text.encodeUtf8 cred)
150 (Text.encodeUtf8 uuid)
152 -- ** Type 'PublicKey'
155 -- | @('publicKey' secKey)@ returns the 'PublicKey'
156 -- derived from given 'SecretKey' @secKey@.
157 publicKey :: Reifies c FFC => SecretKey c -> PublicKey c
158 publicKey = (groupGen ^)