From 786d272fdf723987e582da16e511f067abf671a5 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Thu, 2 Aug 2018 17:26:23 -0400 Subject: Move BTLS.ConstantTimeEquals into the BTLS.BoringSSL hierarchy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s a direct wrapper around a BoringSSL function, so it really belongs there. --- btls.cabal | 2 +- src/BTLS/BoringSSL/Mem.chs | 30 ++++++++++++++++++++++++++++++ src/BTLS/ConstantTimeEquals.chs | 30 ------------------------------ src/Data/HMAC.hs | 4 ++-- 4 files changed, 33 insertions(+), 33 deletions(-) create mode 100644 src/BTLS/BoringSSL/Mem.chs delete mode 100644 src/BTLS/ConstantTimeEquals.chs diff --git a/btls.cabal b/btls.cabal index 1ef7145..809e8be 100644 --- a/btls.cabal +++ b/btls.cabal @@ -74,8 +74,8 @@ library , BTLS.BoringSSL.Digest , BTLS.BoringSSL.HKDF , BTLS.BoringSSL.HMAC + , BTLS.BoringSSL.Mem , BTLS.Cast - , BTLS.ConstantTimeEquals , BTLS.CreateWithFinalizer , BTLS.Result , BTLS.Types diff --git a/src/BTLS/BoringSSL/Mem.chs b/src/BTLS/BoringSSL/Mem.chs new file mode 100644 index 0000000..969cf91 --- /dev/null +++ b/src/BTLS/BoringSSL/Mem.chs @@ -0,0 +1,30 @@ +-- Copyright 2018 Google LLC +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); you may not +-- use this file except in compliance with the License. You may obtain a copy of +-- the License at +-- +-- https://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +-- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +-- License for the specific language governing permissions and limitations under +-- the License. + +module BTLS.BoringSSL.Mem where + +import Foreign (Ptr) +import Foreign.C.Types + +import BTLS.Cast (asVoidPtr) + +#include + +-- | 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. +cryptoMemcmp :: Ptr a -> Ptr a -> Int -> IO Bool +cryptoMemcmp a b size = + let size' = fromIntegral size :: CULong in + (== 0) <$> {#call CRYPTO_memcmp as ^#} (asVoidPtr a) (asVoidPtr b) size' diff --git a/src/BTLS/ConstantTimeEquals.chs b/src/BTLS/ConstantTimeEquals.chs deleted file mode 100644 index 77b1af0..0000000 --- a/src/BTLS/ConstantTimeEquals.chs +++ /dev/null @@ -1,30 +0,0 @@ --- Copyright 2018 Google LLC --- --- Licensed under the Apache License, Version 2.0 (the "License"); you may not --- use this file except in compliance with the License. You may obtain a copy of --- the License at --- --- https://www.apache.org/licenses/LICENSE-2.0 --- --- Unless required by applicable law or agreed to in writing, software --- distributed under the License is distributed on an "AS IS" BASIS, WITHOUT --- WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the --- License for the specific language governing permissions and limitations under --- the License. - -module BTLS.ConstantTimeEquals where - -import Foreign (Ptr) -import Foreign.C.Types - -import BTLS.Cast (asVoidPtr) - -#include - --- | 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 :: CULong in - (== 0) <$> {#call CRYPTO_memcmp as ^#} (asVoidPtr a) (asVoidPtr b) size' diff --git a/src/Data/HMAC.hs b/src/Data/HMAC.hs index a6b72fe..b0387c6 100644 --- a/src/Data/HMAC.hs +++ b/src/Data/HMAC.hs @@ -25,8 +25,8 @@ import Foreign.Marshal.Unsafe (unsafeLocalState) import BTLS.BoringSSL.Base import BTLS.BoringSSL.HMAC +import BTLS.BoringSSL.Mem (cryptoMemcmp) import BTLS.Cast (asCUCharBuf) -import BTLS.ConstantTimeEquals (constantTimeEquals) import BTLS.Types (SecretKey(SecretKey)) import Data.Digest.Internal (Algorithm(Algorithm), Digest(Digest), initUpdateFinalize) @@ -42,7 +42,7 @@ instance Eq HMAC where unsafeLocalState $ ByteString.unsafeUseAsCStringLen a $ \(a', size) -> ByteString.unsafeUseAsCStringLen b $ \(b', _) -> - constantTimeEquals a' b' size + cryptoMemcmp a' b' size instance Show HMAC where show (HMAC m) = show (Digest m) -- cgit v1.2.3