From 2152e7728db7e0cf86ea23d29a05294af24b6898 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Thu, 2 Aug 2018 17:08:44 -0400 Subject: Begin cleaning up internal modules Clarify the division between internal and external modules in the source by consolidating all internal modules except Data.Digest.Internal into a flatter BTLS directory. --- btls.cabal | 20 +++++----- src/BTLS/BoringSSL/Base.chs | 42 +++++++++++++++++++++ src/BTLS/BoringSSL/Digest.chs | 67 ++++++++++++++++++++++++++++++++++ src/BTLS/BoringSSL/HKDF.chs | 45 +++++++++++++++++++++++ src/BTLS/BoringSSL/HMAC.chs | 54 +++++++++++++++++++++++++++ src/BTLS/Cast.hs | 21 +++++++++++ src/BTLS/ConstantTimeEquals.chs | 30 +++++++++++++++ src/BTLS/CreateWithFinalizer.hs | 27 ++++++++++++++ src/BTLS/Result.hs | 27 ++++++++++++++ src/BTLS/Types.hs | 39 ++++++++++++++++++++ src/Codec/Crypto/HKDF.hs | 8 ++-- src/Data/Digest.hs | 4 +- src/Data/Digest/Internal.hs | 4 +- src/Data/HMAC.hs | 8 ++-- src/Foreign/Ptr/Cast.hs | 21 ----------- src/Foreign/Ptr/ConstantTimeEquals.chs | 30 --------------- src/Foreign/Ptr/CreateWithFinalizer.hs | 27 -------------- src/Internal/Base.chs | 42 --------------------- src/Internal/Digest.chs | 67 ---------------------------------- src/Internal/HKDF.chs | 45 ----------------------- src/Internal/HMAC.chs | 54 --------------------------- src/Result.hs | 27 -------------- src/Types.hs | 39 -------------------- 23 files changed, 374 insertions(+), 374 deletions(-) create mode 100644 src/BTLS/BoringSSL/Base.chs create mode 100644 src/BTLS/BoringSSL/Digest.chs create mode 100644 src/BTLS/BoringSSL/HKDF.chs create mode 100644 src/BTLS/BoringSSL/HMAC.chs create mode 100644 src/BTLS/Cast.hs create mode 100644 src/BTLS/ConstantTimeEquals.chs create mode 100644 src/BTLS/CreateWithFinalizer.hs create mode 100644 src/BTLS/Result.hs create mode 100644 src/BTLS/Types.hs delete mode 100644 src/Foreign/Ptr/Cast.hs delete mode 100644 src/Foreign/Ptr/ConstantTimeEquals.chs delete mode 100644 src/Foreign/Ptr/CreateWithFinalizer.hs delete mode 100644 src/Internal/Base.chs delete mode 100644 src/Internal/Digest.chs delete mode 100644 src/Internal/HKDF.chs delete mode 100644 src/Internal/HMAC.chs delete mode 100644 src/Result.hs delete mode 100644 src/Types.hs diff --git a/btls.cabal b/btls.cabal index 02f2ed3..1ef7145 100644 --- a/btls.cabal +++ b/btls.cabal @@ -70,16 +70,16 @@ library exposed-modules: Codec.Crypto.HKDF , Data.Digest , Data.HMAC - other-modules: Data.Digest.Internal - , Foreign.Ptr.Cast - , Foreign.Ptr.ConstantTimeEquals - , Foreign.Ptr.CreateWithFinalizer - , Internal.Base - , Internal.Digest - , Internal.HKDF - , Internal.HMAC - , Result - , Types + other-modules: BTLS.BoringSSL.Base + , BTLS.BoringSSL.Digest + , BTLS.BoringSSL.HKDF + , BTLS.BoringSSL.HMAC + , BTLS.Cast + , BTLS.ConstantTimeEquals + , BTLS.CreateWithFinalizer + , BTLS.Result + , BTLS.Types + , Data.Digest.Internal c-sources: cbits/btls.c -- Use special names for the BoringSSL libraries to avoid accidentally pulling -- in OpenSSL. diff --git a/src/BTLS/BoringSSL/Base.chs b/src/BTLS/BoringSSL/Base.chs new file mode 100644 index 0000000..347e3f4 --- /dev/null +++ b/src/BTLS/BoringSSL/Base.chs @@ -0,0 +1,42 @@ +-- 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. + +{-# OPTIONS_GHC -Wno-unused-imports #-} + +module BTLS.BoringSSL.Base where + +import Foreign (Ptr, nullPtr) + +#include + +-- | The BoringSSL @ENGINE@ type. +data Engine +{#pointer *ENGINE as 'Ptr Engine' -> Engine nocode#} + +noEngine :: Ptr Engine +noEngine = nullPtr + +-- | The BoringSSL @EVP_MD_CTX@ type, representing the state of a pending +-- hashing operation. +data EVPMDCtx +{#pointer *EVP_MD_CTX as 'Ptr EVPMDCtx' -> EVPMDCtx nocode#} + +-- | The BoringSSL @EVP_MD@ type, representing a hash algorithm. +data EVPMD +{#pointer *EVP_MD as 'Ptr EVPMD' -> EVPMD nocode#} + +-- | The BoringSSL @HMAC_CTX@ type, representing the state of a pending HMAC +-- operation. +data HMACCtx +{#pointer *HMAC_CTX as 'Ptr HMACCtx' -> HMACCtx nocode#} diff --git a/src/BTLS/BoringSSL/Digest.chs b/src/BTLS/BoringSSL/Digest.chs new file mode 100644 index 0000000..69f3a0a --- /dev/null +++ b/src/BTLS/BoringSSL/Digest.chs @@ -0,0 +1,67 @@ +-- 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. + +{-# OPTIONS_GHC -Wno-missing-methods #-} + +module BTLS.BoringSSL.Digest + ( evpMD5, evpSHA1, evpSHA224, evpSHA256, evpSHA384, evpSHA512 + , mallocEVPMDCtx + , evpDigestInitEx, evpDigestUpdate, evpDigestFinalEx + , evpMaxMDSize + ) where + +import Foreign (FinalizerPtr, ForeignPtr, Ptr, Storable(alignment, sizeOf)) +import Foreign.C.Types + +{#import BTLS.BoringSSL.Base#} +import BTLS.Cast (asVoidPtr) +import BTLS.CreateWithFinalizer (createWithFinalizer) +import BTLS.Result + +#include + +evpMD5, evpSHA1, evpSHA224, evpSHA256, evpSHA384, evpSHA512 :: Ptr EVPMD +evpMD5 = {#call pure EVP_md5 as ^#} +evpSHA1 = {#call pure EVP_sha1 as ^#} +evpSHA224 = {#call pure EVP_sha224 as ^#} +evpSHA256 = {#call pure EVP_sha256 as ^#} +evpSHA384 = {#call pure EVP_sha384 as ^#} +evpSHA512 = {#call pure EVP_sha512 as ^#} + +-- | Memory-safe allocator for 'EVPMDCtx'. +mallocEVPMDCtx :: IO (ForeignPtr EVPMDCtx) +mallocEVPMDCtx = + createWithFinalizer {#call EVP_MD_CTX_init as ^#} btlsFinalizeEVPMDCtxPtr + +foreign import ccall "&btlsFinalizeEVPMDCtx" + btlsFinalizeEVPMDCtxPtr :: FinalizerPtr EVPMDCtx + +evpDigestInitEx :: Ptr EVPMDCtx -> Ptr EVPMD -> Ptr Engine -> IO () +evpDigestInitEx ctx md engine = + requireSuccess $ {#call EVP_DigestInit_ex as ^#} ctx md engine + +evpDigestUpdate :: Ptr EVPMDCtx -> Ptr a -> CULong -> IO () +evpDigestUpdate ctx md bytes = + alwaysSucceeds $ {#call EVP_DigestUpdate as ^#} ctx (asVoidPtr md) bytes + +evpDigestFinalEx :: Ptr EVPMDCtx -> Ptr CUChar -> Ptr CUInt -> IO () +evpDigestFinalEx ctx mdOut outSize = + alwaysSucceeds $ {#call EVP_DigestFinal_ex as ^#} ctx mdOut outSize + +evpMaxMDSize :: Int +evpMaxMDSize = {#const EVP_MAX_MD_SIZE#} + +instance Storable EVPMDCtx where + sizeOf _ = {#sizeof EVP_MD_CTX#} + alignment _ = {#alignof EVP_MD_CTX#} diff --git a/src/BTLS/BoringSSL/HKDF.chs b/src/BTLS/BoringSSL/HKDF.chs new file mode 100644 index 0000000..3710c0c --- /dev/null +++ b/src/BTLS/BoringSSL/HKDF.chs @@ -0,0 +1,45 @@ +-- 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.HKDF + ( hkdfExtract, hkdfExpand + ) where + +import Foreign (Ptr) +import Foreign.C.Types + +{#import BTLS.BoringSSL.Base#} +import BTLS.Result + +#include + +hkdfExtract :: + Ptr CUChar -> Ptr CULong + -> Ptr EVPMD + -> Ptr CUChar -> CULong + -> Ptr CUChar -> CULong + -> IO () +hkdfExtract outKey outLen digest secret secretLen salt saltLen = + requireSuccess $ + {#call HKDF_extract as ^#} outKey outLen digest secret secretLen salt saltLen + +hkdfExpand :: + Ptr CUChar -> CULong + -> Ptr EVPMD + -> Ptr CUChar -> CULong + -> Ptr CUChar -> CULong + -> IO () +hkdfExpand outKey outLen digest prk prkLen info infoLen = + requireSuccess $ + {#call HKDF_expand as ^#} outKey outLen digest prk prkLen info infoLen diff --git a/src/BTLS/BoringSSL/HMAC.chs b/src/BTLS/BoringSSL/HMAC.chs new file mode 100644 index 0000000..5c53122 --- /dev/null +++ b/src/BTLS/BoringSSL/HMAC.chs @@ -0,0 +1,54 @@ +-- 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. + +{-# OPTIONS_GHC -Wno-missing-methods #-} + +module BTLS.BoringSSL.HMAC + ( mallocHMACCtx + , hmacInitEx, hmacUpdate, hmacFinal + ) where + +import Foreign (FinalizerPtr, ForeignPtr, Ptr, Storable(alignment, sizeOf)) +import Foreign.C.Types + +{#import BTLS.BoringSSL.Base#} +import BTLS.Cast (asVoidPtr) +import BTLS.CreateWithFinalizer (createWithFinalizer) +import BTLS.Result + +#include + +-- | Memory-safe allocator for 'HMACCtx'. +mallocHMACCtx :: IO (ForeignPtr HMACCtx) +mallocHMACCtx = createWithFinalizer {#call HMAC_CTX_init as ^#} hmacCtxCleanup + +foreign import ccall "&HMAC_CTX_cleanup" + hmacCtxCleanup :: FinalizerPtr HMACCtx + +hmacInitEx :: Ptr HMACCtx -> Ptr a -> CULong -> Ptr EVPMD -> Ptr Engine -> IO () +hmacInitEx ctx bytes size md engine = + requireSuccess $ + {#call HMAC_Init_ex as ^#} ctx (asVoidPtr bytes) size md engine + +hmacUpdate :: Ptr HMACCtx -> Ptr CUChar -> CULong -> IO () +hmacUpdate ctx bytes size = + alwaysSucceeds $ {#call HMAC_Update as ^#} ctx bytes size + +hmacFinal :: Ptr HMACCtx -> Ptr CUChar -> Ptr CUInt -> IO () +hmacFinal ctx out outSize = + requireSuccess $ {#call HMAC_Final as ^#} ctx out outSize + +instance Storable HMACCtx where + sizeOf _ = {#sizeof HMAC_CTX#} + alignment _ = {#alignof HMAC_CTX#} diff --git a/src/BTLS/Cast.hs b/src/BTLS/Cast.hs new file mode 100644 index 0000000..6f29469 --- /dev/null +++ b/src/BTLS/Cast.hs @@ -0,0 +1,21 @@ +-- 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.Cast where + +import Foreign (Ptr) +import Unsafe.Coerce (unsafeCoerce) + +asVoidPtr :: Ptr a -> Ptr () +asVoidPtr = unsafeCoerce diff --git a/src/BTLS/ConstantTimeEquals.chs b/src/BTLS/ConstantTimeEquals.chs new file mode 100644 index 0000000..77b1af0 --- /dev/null +++ b/src/BTLS/ConstantTimeEquals.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.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/BTLS/CreateWithFinalizer.hs b/src/BTLS/CreateWithFinalizer.hs new file mode 100644 index 0000000..3862225 --- /dev/null +++ b/src/BTLS/CreateWithFinalizer.hs @@ -0,0 +1,27 @@ +-- 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.CreateWithFinalizer (createWithFinalizer) where + +import Foreign + (FinalizerPtr, ForeignPtr, Ptr, Storable, addForeignPtrFinalizer, + mallocForeignPtr, withForeignPtr) + +createWithFinalizer :: + Storable a => (Ptr a -> IO ()) -> FinalizerPtr a -> IO (ForeignPtr a) +createWithFinalizer initialize finalize = do + fp <- mallocForeignPtr + withForeignPtr fp initialize + addForeignPtrFinalizer finalize fp + return fp diff --git a/src/BTLS/Result.hs b/src/BTLS/Result.hs new file mode 100644 index 0000000..b9ad4a7 --- /dev/null +++ b/src/BTLS/Result.hs @@ -0,0 +1,27 @@ +-- 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.Result where + +import Control.Exception (assert) +import Foreign (throwIf_) +import Foreign.C.Types + +alwaysSucceeds :: IO CInt -> IO () +alwaysSucceeds f = do + r <- f + assert (r == 1) (return ()) + +requireSuccess :: IO CInt -> IO () +requireSuccess f = throwIf_ (/= 1) (const "BoringSSL failure") f diff --git a/src/BTLS/Types.hs b/src/BTLS/Types.hs new file mode 100644 index 0000000..dbd806a --- /dev/null +++ b/src/BTLS/Types.hs @@ -0,0 +1,39 @@ +-- 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.Types + ( AssociatedData(AssociatedData) + , Salt(Salt), noSalt + , SecretKey(SecretKey) + ) where + +import Data.ByteString (ByteString) +import qualified Data.ByteString as ByteString + +-- | Context or application-specific information. Equality comparisons on this +-- type are variable-time. +newtype AssociatedData = AssociatedData ByteString + deriving (Eq, Ord, Show) + +-- | A salt. Equality comparisons on this type are variable-time. +newtype Salt = Salt ByteString + deriving (Eq, Ord, Show) + +noSalt :: Salt +noSalt = Salt ByteString.empty + +-- | A secret key used as input to a cipher or HMAC. Equality comparisons on +-- this type are variable-time. +newtype SecretKey = SecretKey ByteString + deriving (Eq, Ord, Show) diff --git a/src/Codec/Crypto/HKDF.hs b/src/Codec/Crypto/HKDF.hs index 1c1dbda..8c1db0a 100644 --- a/src/Codec/Crypto/HKDF.hs +++ b/src/Codec/Crypto/HKDF.hs @@ -24,11 +24,11 @@ import Foreign.C.Types import Foreign.Marshal.Unsafe (unsafeLocalState) import Unsafe.Coerce (unsafeCoerce) -import Data.Digest.Internal (Algorithm(Algorithm)) -import Internal.Digest (evpMaxMDSize) -import Internal.HKDF -import Types +import BTLS.BoringSSL.Digest (evpMaxMDSize) +import BTLS.BoringSSL.HKDF +import BTLS.Types (AssociatedData(AssociatedData), Salt(Salt), SecretKey(SecretKey), noSalt) +import Data.Digest.Internal (Algorithm(Algorithm)) -- | Computes an HKDF pseudorandom key (PRK) as specified by RFC 5869. extract :: Algorithm -> Salt -> SecretKey -> SecretKey diff --git a/src/Data/Digest.hs b/src/Data/Digest.hs index b5c7390..2d06389 100644 --- a/src/Data/Digest.hs +++ b/src/Data/Digest.hs @@ -27,9 +27,9 @@ import Foreign.C.Types import Foreign.Marshal.Unsafe (unsafeLocalState) import Unsafe.Coerce (unsafeCoerce) +import BTLS.BoringSSL.Base +import BTLS.BoringSSL.Digest import Data.Digest.Internal -import Internal.Base -import Internal.Digest type LazyByteString = ByteString.Lazy.ByteString diff --git a/src/Data/Digest/Internal.hs b/src/Data/Digest/Internal.hs index 02b879c..88a0d9f 100644 --- a/src/Data/Digest/Internal.hs +++ b/src/Data/Digest/Internal.hs @@ -28,8 +28,8 @@ import Data.Word (Word8) import Foreign (ForeignPtr, Storable(peek), Ptr, alloca, allocaArray, withForeignPtr) import Foreign.C.Types -import Internal.Base (EVPMD) -import Internal.Digest (evpMaxMDSize) +import BTLS.BoringSSL.Base (EVPMD) +import BTLS.BoringSSL.Digest (evpMaxMDSize) type LazyByteString = ByteString.Lazy.ByteString diff --git a/src/Data/HMAC.hs b/src/Data/HMAC.hs index 85e6886..29b6ce0 100644 --- a/src/Data/HMAC.hs +++ b/src/Data/HMAC.hs @@ -26,12 +26,12 @@ import Foreign.C.Types import Foreign.Marshal.Unsafe (unsafeLocalState) import Unsafe.Coerce (unsafeCoerce) +import BTLS.BoringSSL.Base +import BTLS.BoringSSL.HMAC +import BTLS.ConstantTimeEquals (constantTimeEquals) +import BTLS.Types (SecretKey(SecretKey)) import Data.Digest.Internal (Algorithm(Algorithm), Digest(Digest), initUpdateFinalize) -import Foreign.Ptr.ConstantTimeEquals (constantTimeEquals) -import Internal.Base -import Internal.HMAC -import Types (SecretKey(SecretKey)) type LazyByteString = ByteString.Lazy.ByteString diff --git a/src/Foreign/Ptr/Cast.hs b/src/Foreign/Ptr/Cast.hs deleted file mode 100644 index 653604a..0000000 --- a/src/Foreign/Ptr/Cast.hs +++ /dev/null @@ -1,21 +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 Foreign.Ptr.Cast where - -import Foreign (Ptr) -import Unsafe.Coerce (unsafeCoerce) - -asVoidPtr :: Ptr a -> Ptr () -asVoidPtr = unsafeCoerce diff --git a/src/Foreign/Ptr/ConstantTimeEquals.chs b/src/Foreign/Ptr/ConstantTimeEquals.chs deleted file mode 100644 index a96fc66..0000000 --- a/src/Foreign/Ptr/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 Foreign.Ptr.ConstantTimeEquals where - -import Foreign (Ptr) -import Foreign.C.Types - -import Foreign.Ptr.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/Foreign/Ptr/CreateWithFinalizer.hs b/src/Foreign/Ptr/CreateWithFinalizer.hs deleted file mode 100644 index b1dd583..0000000 --- a/src/Foreign/Ptr/CreateWithFinalizer.hs +++ /dev/null @@ -1,27 +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 Foreign.Ptr.CreateWithFinalizer (createWithFinalizer) where - -import Foreign - (FinalizerPtr, ForeignPtr, Ptr, Storable, addForeignPtrFinalizer, - mallocForeignPtr, withForeignPtr) - -createWithFinalizer :: - Storable a => (Ptr a -> IO ()) -> FinalizerPtr a -> IO (ForeignPtr a) -createWithFinalizer initialize finalize = do - fp <- mallocForeignPtr - withForeignPtr fp initialize - addForeignPtrFinalizer finalize fp - return fp diff --git a/src/Internal/Base.chs b/src/Internal/Base.chs deleted file mode 100644 index f0b03cc..0000000 --- a/src/Internal/Base.chs +++ /dev/null @@ -1,42 +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. - -{-# OPTIONS_GHC -Wno-unused-imports #-} - -module Internal.Base where - -import Foreign (Ptr, nullPtr) - -#include - --- | The BoringSSL @ENGINE@ type. -data Engine -{#pointer *ENGINE as 'Ptr Engine' -> Engine nocode#} - -noEngine :: Ptr Engine -noEngine = nullPtr - --- | The BoringSSL @EVP_MD_CTX@ type, representing the state of a pending --- hashing operation. -data EVPMDCtx -{#pointer *EVP_MD_CTX as 'Ptr EVPMDCtx' -> EVPMDCtx nocode#} - --- | The BoringSSL @EVP_MD@ type, representing a hash algorithm. -data EVPMD -{#pointer *EVP_MD as 'Ptr EVPMD' -> EVPMD nocode#} - --- | The BoringSSL @HMAC_CTX@ type, representing the state of a pending HMAC --- operation. -data HMACCtx -{#pointer *HMAC_CTX as 'Ptr HMACCtx' -> HMACCtx nocode#} diff --git a/src/Internal/Digest.chs b/src/Internal/Digest.chs deleted file mode 100644 index bd331a7..0000000 --- a/src/Internal/Digest.chs +++ /dev/null @@ -1,67 +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. - -{-# OPTIONS_GHC -Wno-missing-methods #-} - -module Internal.Digest - ( evpMD5, evpSHA1, evpSHA224, evpSHA256, evpSHA384, evpSHA512 - , mallocEVPMDCtx - , evpDigestInitEx, evpDigestUpdate, evpDigestFinalEx - , evpMaxMDSize - ) where - -import Foreign (FinalizerPtr, ForeignPtr, Ptr, Storable(alignment, sizeOf)) -import Foreign.C.Types -import Foreign.Ptr.Cast (asVoidPtr) - -import Foreign.Ptr.CreateWithFinalizer (createWithFinalizer) -{#import Internal.Base#} -import Result - -#include - -evpMD5, evpSHA1, evpSHA224, evpSHA256, evpSHA384, evpSHA512 :: Ptr EVPMD -evpMD5 = {#call pure EVP_md5 as ^#} -evpSHA1 = {#call pure EVP_sha1 as ^#} -evpSHA224 = {#call pure EVP_sha224 as ^#} -evpSHA256 = {#call pure EVP_sha256 as ^#} -evpSHA384 = {#call pure EVP_sha384 as ^#} -evpSHA512 = {#call pure EVP_sha512 as ^#} - --- | Memory-safe allocator for 'EVPMDCtx'. -mallocEVPMDCtx :: IO (ForeignPtr EVPMDCtx) -mallocEVPMDCtx = - createWithFinalizer {#call EVP_MD_CTX_init as ^#} btlsFinalizeEVPMDCtxPtr - -foreign import ccall "&btlsFinalizeEVPMDCtx" - btlsFinalizeEVPMDCtxPtr :: FinalizerPtr EVPMDCtx - -evpDigestInitEx :: Ptr EVPMDCtx -> Ptr EVPMD -> Ptr Engine -> IO () -evpDigestInitEx ctx md engine = - requireSuccess $ {#call EVP_DigestInit_ex as ^#} ctx md engine - -evpDigestUpdate :: Ptr EVPMDCtx -> Ptr a -> CULong -> IO () -evpDigestUpdate ctx md bytes = - alwaysSucceeds $ {#call EVP_DigestUpdate as ^#} ctx (asVoidPtr md) bytes - -evpDigestFinalEx :: Ptr EVPMDCtx -> Ptr CUChar -> Ptr CUInt -> IO () -evpDigestFinalEx ctx mdOut outSize = - alwaysSucceeds $ {#call EVP_DigestFinal_ex as ^#} ctx mdOut outSize - -evpMaxMDSize :: Int -evpMaxMDSize = {#const EVP_MAX_MD_SIZE#} - -instance Storable EVPMDCtx where - sizeOf _ = {#sizeof EVP_MD_CTX#} - alignment _ = {#alignof EVP_MD_CTX#} diff --git a/src/Internal/HKDF.chs b/src/Internal/HKDF.chs deleted file mode 100644 index a3a48ed..0000000 --- a/src/Internal/HKDF.chs +++ /dev/null @@ -1,45 +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 Internal.HKDF - ( hkdfExtract, hkdfExpand - ) where - -import Foreign (Ptr) -import Foreign.C.Types - -{#import Internal.Base#} -import Result - -#include - -hkdfExtract :: - Ptr CUChar -> Ptr CULong - -> Ptr EVPMD - -> Ptr CUChar -> CULong - -> Ptr CUChar -> CULong - -> IO () -hkdfExtract outKey outLen digest secret secretLen salt saltLen = - requireSuccess $ - {#call HKDF_extract as ^#} outKey outLen digest secret secretLen salt saltLen - -hkdfExpand :: - Ptr CUChar -> CULong - -> Ptr EVPMD - -> Ptr CUChar -> CULong - -> Ptr CUChar -> CULong - -> IO () -hkdfExpand outKey outLen digest prk prkLen info infoLen = - requireSuccess $ - {#call HKDF_expand as ^#} outKey outLen digest prk prkLen info infoLen diff --git a/src/Internal/HMAC.chs b/src/Internal/HMAC.chs deleted file mode 100644 index 2151785..0000000 --- a/src/Internal/HMAC.chs +++ /dev/null @@ -1,54 +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. - -{-# OPTIONS_GHC -Wno-missing-methods #-} - -module Internal.HMAC - ( mallocHMACCtx - , hmacInitEx, hmacUpdate, hmacFinal - ) where - -import Foreign (FinalizerPtr, ForeignPtr, Ptr, Storable(alignment, sizeOf)) -import Foreign.C.Types - -import Foreign.Ptr.Cast (asVoidPtr) -import Foreign.Ptr.CreateWithFinalizer (createWithFinalizer) -{#import Internal.Base#} -import Result - -#include - --- | Memory-safe allocator for 'HMACCtx'. -mallocHMACCtx :: IO (ForeignPtr HMACCtx) -mallocHMACCtx = createWithFinalizer {#call HMAC_CTX_init as ^#} hmacCtxCleanup - -foreign import ccall "&HMAC_CTX_cleanup" - hmacCtxCleanup :: FinalizerPtr HMACCtx - -hmacInitEx :: Ptr HMACCtx -> Ptr a -> CULong -> Ptr EVPMD -> Ptr Engine -> IO () -hmacInitEx ctx bytes size md engine = - requireSuccess $ - {#call HMAC_Init_ex as ^#} ctx (asVoidPtr bytes) size md engine - -hmacUpdate :: Ptr HMACCtx -> Ptr CUChar -> CULong -> IO () -hmacUpdate ctx bytes size = - alwaysSucceeds $ {#call HMAC_Update as ^#} ctx bytes size - -hmacFinal :: Ptr HMACCtx -> Ptr CUChar -> Ptr CUInt -> IO () -hmacFinal ctx out outSize = - requireSuccess $ {#call HMAC_Final as ^#} ctx out outSize - -instance Storable HMACCtx where - sizeOf _ = {#sizeof HMAC_CTX#} - alignment _ = {#alignof HMAC_CTX#} diff --git a/src/Result.hs b/src/Result.hs deleted file mode 100644 index dfd3b9f..0000000 --- a/src/Result.hs +++ /dev/null @@ -1,27 +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 Result where - -import Control.Exception (assert) -import Foreign (throwIf_) -import Foreign.C.Types - -alwaysSucceeds :: IO CInt -> IO () -alwaysSucceeds f = do - r <- f - assert (r == 1) (return ()) - -requireSuccess :: IO CInt -> IO () -requireSuccess f = throwIf_ (/= 1) (const "BoringSSL failure") f diff --git a/src/Types.hs b/src/Types.hs deleted file mode 100644 index 3c0f350..0000000 --- a/src/Types.hs +++ /dev/null @@ -1,39 +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 Types - ( AssociatedData(AssociatedData) - , Salt(Salt), noSalt - , SecretKey(SecretKey) - ) where - -import Data.ByteString (ByteString) -import qualified Data.ByteString as ByteString - --- | Context or application-specific information. Equality comparisons on this --- type are variable-time. -newtype AssociatedData = AssociatedData ByteString - deriving (Eq, Ord, Show) - --- | A salt. Equality comparisons on this type are variable-time. -newtype Salt = Salt ByteString - deriving (Eq, Ord, Show) - -noSalt :: Salt -noSalt = Salt ByteString.empty - --- | A secret key used as input to a cipher or HMAC. Equality comparisons on --- this type are variable-time. -newtype SecretKey = SecretKey ByteString - deriving (Eq, Ord, Show) -- cgit v1.2.3