diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Data/Digest/Internal.chs | 12 | ||||
-rw-r--r-- | src/Data/Hmac.chs | 5 | ||||
-rw-r--r-- | src/Result.hs | 27 |
3 files changed, 32 insertions, 12 deletions
diff --git a/src/Data/Digest/Internal.chs b/src/Data/Digest/Internal.chs index ed4e09e..86cea65 100644 --- a/src/Data/Digest/Internal.chs +++ b/src/Data/Digest/Internal.chs @@ -16,7 +16,6 @@ module Data.Digest.Internal where -import Control.Exception (assert) import Data.Bits (Bits((.&.)), shiftR) import Data.ByteString (ByteString) import qualified Data.ByteString as ByteString @@ -27,12 +26,13 @@ import Data.Word (Word8) import Foreign (FinalizerPtr, ForeignPtr, Ptr, Storable(alignment, peek, sizeOf), addForeignPtrFinalizer, alloca, allocaArray, mallocForeignPtr, - nullPtr, throwIf_, withForeignPtr) + nullPtr, withForeignPtr) import Foreign.C.Types import Foreign.Marshal.Unsafe (unsafeLocalState) import Unsafe.Coerce (unsafeCoerce) import Foreign.Ptr.Cast (asVoidPtr) +import Result type LazyByteString = ByteString.Lazy.ByteString @@ -70,11 +70,6 @@ evpMaxMdSize = {#const EVP_MAX_MD_SIZE#} -- Some of these functions return 'CInt' even though they can never fail. Wrap -- them to prevent warnings. -alwaysSucceeds :: IO CInt -> IO () -alwaysSucceeds f = do - r <- f - assert (r == 1) (return ()) - evpDigestUpdate :: Ptr EvpMdCtx -> Ptr a -> CULong -> IO () evpDigestUpdate ctx md bytes = alwaysSucceeds $ {#call EVP_DigestUpdate as ^#} ctx (asVoidPtr md) bytes @@ -85,9 +80,6 @@ evpDigestFinalEx ctx mdOut outSize = -- Convert functions that can in fact fail to throw exceptions instead. -requireSuccess :: IO CInt -> IO () -requireSuccess f = throwIf_ (/= 1) (const "BoringSSL failure") f - evpDigestInitEx :: Ptr EvpMdCtx -> Ptr EvpMd -> Ptr Engine -> IO () evpDigestInitEx ctx md engine = requireSuccess $ {#call EVP_DigestInit_ex as ^#} ctx md engine diff --git a/src/Data/Hmac.chs b/src/Data/Hmac.chs index 7ee68d2..907c352 100644 --- a/src/Data/Hmac.chs +++ b/src/Data/Hmac.chs @@ -33,10 +33,11 @@ import Foreign.Marshal.Unsafe (unsafeLocalState) import Unsafe.Coerce (unsafeCoerce) {#import Data.Digest.Internal#} - (Algorithm(Algorithm), Digest(Digest), Engine, EvpMd, - alwaysSucceeds, evpMaxMdSize, noEngine, requireSuccess) + (Algorithm(Algorithm), Digest(Digest), Engine, EvpMd, evpMaxMdSize, + noEngine) import Foreign.Ptr.Cast (asVoidPtr) {#import Foreign.Ptr.ConstantTimeEquals#} (constantTimeEquals) +import Result type LazyByteString = ByteString.Lazy.ByteString diff --git a/src/Result.hs b/src/Result.hs new file mode 100644 index 0000000..dfd3b9f --- /dev/null +++ b/src/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 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 |