1 {-# LANGUAGE DeriveAnyClass #-}
2 {-# LANGUAGE DeriveGeneric #-}
3 module Voting.Protocol.Credential where
5 import Control.DeepSeq (NFData)
6 import Control.Monad (Monad(..), replicateM)
9 import Data.Char (Char)
10 import Data.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.Text (Text)
18 import GHC.Generics (Generic)
19 import Numeric.Natural (Natural)
20 import Prelude (Integral(..), fromIntegral, div)
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.ByteArray as ByteArray
25 import qualified Data.ByteString as BS
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.Arithmetic
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,NFData)
44 credentialAlphabet :: [Char] -- TODO: make this an array
45 credentialAlphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
47 tokenBase = List.length credentialAlphabet
51 -- | @'randomCredential'@ generates a random 'Credential'.
55 S.StateT r m Credential
57 rs <- replicateM tokenLength (randomR (fromIntegral tokenBase))
58 let (tot, cs) = List.foldl' (\(acc,ds) d ->
60 , charOfDigit d : ds )
62 let checksum = (neg tot + 53) `mod` 53 -- NOTE: why 53 and not 'tokenBase' ?
63 return $ Credential $ Text.reverse $ Text.pack (charOfDigit checksum:cs)
65 charOfDigit = (credentialAlphabet List.!!)
67 -- | @'readCredential'@ reads and check the well-formedness of a 'Credential'
69 readCredential :: Text -> Either CredentialError Credential
71 | Text.length s /= tokenLength + 1 = Left CredentialError_Length
74 (\acc c -> acc >>= \a -> ((a * tokenBase) +) <$> digitOfChar c)
77 checksum <- digitOfChar (Text.last s)
78 if (tot + checksum) `mod` 53 == 0
79 then Right (Credential s)
80 else Left CredentialError_Checksum
83 maybe (Left $ CredentialError_BadChar c) Right $
84 List.elemIndex c credentialAlphabet
86 -- ** Type 'CredentialError'
88 = CredentialError_BadChar Char.Char
89 | CredentialError_Checksum
90 | CredentialError_Length
91 deriving (Eq,Show,Generic,NFData)
94 newtype UUID = UUID Text
95 deriving (Eq,Ord,Show,Generic,NFData)
97 -- | @'randomUUID'@ generates a random 'UUID'.
100 Random.RandomGen r =>
103 rs <- replicateM tokenLength (randomR (fromIntegral tokenBase))
104 return $ UUID $ Text.pack $ charOfDigit <$> rs
106 charOfDigit = (credentialAlphabet List.!!)
108 -- ** Type 'SecretKey'
111 -- | @('credentialSecretKey' uuid cred)@ returns the 'SecretKey'
112 -- derived from given 'uuid' and 'cred'
113 -- using 'Crypto.fastPBKDF2_SHA256'.
114 credentialSecretKey :: SubGroup q => UUID -> Credential -> SecretKey q
115 credentialSecretKey (UUID uuid) (Credential cred) =
116 fromNatural $ BS.foldl'
117 (\acc b -> acc`shiftL`3 + fromIntegral b)
119 (ByteArray.convert deriv)
121 deriv :: BS.ByteString
123 Crypto.fastPBKDF2_SHA256
125 { Crypto.iterCounts = 1000
126 , Crypto.outputLength = 256 `div` 8
128 (Text.encodeUtf8 cred)
129 (Text.encodeUtf8 uuid)
131 -- ** Type 'PublicKey'
134 -- | @('publicKey' secKey)@ returns the 'PublicKey'
135 -- derived from given 'SecretKey'.
136 publicKey :: SubGroup q => SecretKey q -> PublicKey q
137 publicKey = (groupGen ^)