Skip to content

Commit

Permalink
Untested timing attack fix for pwstore-fast
Browse files Browse the repository at this point in the history
  • Loading branch information
oakwhiz committed Feb 24, 2014
1 parent e4ff3cc commit 21b14c0
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pwstore-fast/Crypto/PasswordStore.hs
Expand Up @@ -119,6 +119,9 @@ import System.IO
import System.Random
import Data.Maybe
import qualified Control.Exception
import Data.Char
import Data.List
import Data.Function

---------------------
-- Cryptographic base
Expand Down Expand Up @@ -296,6 +299,12 @@ makePasswordSaltWith algorithm strengthModifier pwd salt strength = writePwHash
makePasswordSalt :: ByteString -> Salt -> Int -> ByteString
makePasswordSalt = makePasswordSaltWith pbkdf1 (2^)

-- | Constant-time comparison function to use instead of == when comparing with a secret
constantTimeCompare a b =
((==) `on` length) a b && 0 == (foldl1 (.|.) joined)
where
joined = zipWith (xor `on` ord) a b

-- | 'verifyPasswordWith' @algorithm userInput pwHash@ verifies
-- the password @userInput@ given by the user against the stored password
-- hash @pwHash@, with the hashing algorithm @algorithm@. Returns 'True' if the
Expand All @@ -322,7 +331,7 @@ verifyPasswordWith algorithm strengthModifier userInput pwHash =
case readPwHash pwHash of
Nothing -> False
Just (strength, salt, goodHash) ->
encode (algorithm userInput salt (strengthModifier strength)) == goodHash
encode (algorithm userInput salt (strengthModifier strength)) `constantTimeCompare` goodHash

-- | Like 'verifyPasswordWith', but uses 'pbkdf1' as algorithm.
verifyPassword :: ByteString -> ByteString -> Bool
Expand Down

0 comments on commit 21b14c0

Please sign in to comment.