1-- |
2-- Module      : Crypto.PubKey.Internal
3-- License     : BSD-style
4-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
5-- Stability   : experimental
6-- Portability : Good
7--
8module Crypto.PubKey.Internal
9    ( and'
10    , (&&!)
11    , dsaTruncHash
12    , dsaTruncHashDigest
13    ) where
14
15import Data.Bits (shiftR)
16import Data.List (foldl')
17
18import Crypto.Hash
19import Crypto.Internal.ByteArray (ByteArrayAccess)
20import Crypto.Number.Basic (numBits)
21import Crypto.Number.Serialize
22
23-- | This is a strict version of and
24and' :: [Bool] -> Bool
25and' l = foldl' (&&!) True l
26
27-- | This is a strict version of &&.
28(&&!) :: Bool -> Bool -> Bool
29True  &&! True  = True
30True  &&! False = False
31False &&! True  = False
32False &&! False = False
33
34-- | Truncate and hash for DSA and ECDSA.
35dsaTruncHash :: (ByteArrayAccess msg, HashAlgorithm hash) => hash -> msg -> Integer -> Integer
36dsaTruncHash hashAlg = dsaTruncHashDigest . hashWith hashAlg
37
38-- | Truncate a digest for DSA and ECDSA.
39dsaTruncHashDigest :: HashAlgorithm hash => Digest hash -> Integer -> Integer
40dsaTruncHashDigest digest n
41    | d > 0 = shiftR e d
42    | otherwise = e
43  where e = os2ip digest
44        d = hashDigestSize (getHashAlg digest) * 8 - numBits n
45
46getHashAlg :: Digest hash -> hash
47getHashAlg _ = undefined
48