aboutsummaryrefslogtreecommitdiff
path: root/src/Foreign/Ptr/ConstantTimeEquals.hs
blob: bb8d2d435b0f1bcce3398043ce7e15dd1a7617e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{-# LANGUAGE ScopedTypeVariables #-}

module Foreign.Ptr.ConstantTimeEquals where

import Foreign (Ptr)
import Foreign.C.Types

foreign import ccall "openssl/mem.h CRYPTO_memcmp"
  cryptoMemcmp :: Ptr a -> Ptr a -> CSize -> IO CInt

-- | Directly compares two buffers for equality. This operation takes an amount
-- of time dependent on the specified size but independent of either buffer's
-- contents.
constantTimeEquals :: Ptr a -> Ptr a -> Int -> IO Bool
constantTimeEquals a b size =
  let size' = fromIntegral size :: CSize
  in (== 0) <$> cryptoMemcmp a b size'