aboutsummaryrefslogtreecommitdiff
path: root/src/Foreign/Ptr/ConstantTimeEquals.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Foreign/Ptr/ConstantTimeEquals.hs')
-rw-r--r--src/Foreign/Ptr/ConstantTimeEquals.hs17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/Foreign/Ptr/ConstantTimeEquals.hs b/src/Foreign/Ptr/ConstantTimeEquals.hs
new file mode 100644
index 0000000..bb8d2d4
--- /dev/null
+++ b/src/Foreign/Ptr/ConstantTimeEquals.hs
@@ -0,0 +1,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'