From 46f0b4f51ffa6982e66bdbf3a29426fb15c999d2 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Sat, 28 Apr 2018 14:28:03 -0700 Subject: Begin refactoring low-level foreign imports into their own hierarchy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s clear at this point that this system is going to get large enough that a two-layer implementation is warranted. `Internal` will contain low-level bindings to BoringSSL; other modules will export an idiomatic interface on top of them. --- btls.cabal | 2 ++ src/Data/Digest.chs | 1 + src/Data/Digest/Internal.chs | 29 ++++------------------------- src/Data/Hmac.chs | 4 ++-- src/Internal/Base.chs | 38 ++++++++++++++++++++++++++++++++++++++ src/Internal/Digest.chs | 28 ++++++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 27 deletions(-) create mode 100644 src/Internal/Base.chs create mode 100644 src/Internal/Digest.chs diff --git a/btls.cabal b/btls.cabal index 0359ab7..4f46ec3 100644 --- a/btls.cabal +++ b/btls.cabal @@ -56,6 +56,8 @@ library other-modules: Data.Digest.Internal , Foreign.Ptr.Cast , Foreign.Ptr.ConstantTimeEquals + , Internal.Base + , Internal.Digest , Result c-sources: cbits/btls.c -- Use special names for the BoringSSL libraries to avoid accidentally pulling diff --git a/src/Data/Digest.chs b/src/Data/Digest.chs index 09ab518..0ed24d2 100644 --- a/src/Data/Digest.chs +++ b/src/Data/Digest.chs @@ -27,6 +27,7 @@ module Data.Digest import Foreign (Ptr) {#import Data.Digest.Internal#} +{#import Internal.Base#} #include diff --git a/src/Data/Digest/Internal.chs b/src/Data/Digest/Internal.chs index 86cea65..6478810 100644 --- a/src/Data/Digest/Internal.chs +++ b/src/Data/Digest/Internal.chs @@ -12,8 +12,6 @@ -- License for the specific language governing permissions and limitations under -- the License. -{-# OPTIONS_GHC -Wno-missing-methods #-} - module Data.Digest.Internal where import Data.Bits (Bits((.&.)), shiftR) @@ -24,13 +22,14 @@ import qualified Data.ByteString.Unsafe as ByteString import Data.Char (intToDigit) import Data.Word (Word8) import Foreign - (FinalizerPtr, ForeignPtr, Ptr, Storable(alignment, peek, sizeOf), - addForeignPtrFinalizer, alloca, allocaArray, mallocForeignPtr, - nullPtr, withForeignPtr) + (FinalizerPtr, ForeignPtr, Ptr, Storable(peek), addForeignPtrFinalizer, + alloca, allocaArray, mallocForeignPtr, withForeignPtr) import Foreign.C.Types import Foreign.Marshal.Unsafe (unsafeLocalState) import Unsafe.Coerce (unsafeCoerce) +{#import Internal.Base#} +{#import Internal.Digest#} () import Foreign.Ptr.Cast (asVoidPtr) import Result @@ -40,26 +39,6 @@ type LazyByteString = ByteString.Lazy.ByteString -- First, we build basic bindings to the BoringSSL EVP interface. --- | The BoringSSL @ENGINE@ type. -data Engine -{#pointer *ENGINE as 'Ptr Engine' -> Engine nocode#} - -noEngine :: Ptr Engine -noEngine = nullPtr - --- | The BoringSSL @EVP_MD@ type, representing a hash algorithm. -data EvpMd -{#pointer *EVP_MD as 'Ptr EvpMd' -> EvpMd nocode#} - --- | 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#} - -instance Storable EvpMdCtx where - sizeOf _ = {#sizeof EVP_MD_CTX#} - alignment _ = {#alignof EVP_MD_CTX#} - -- Imported functions from BoringSSL. See -- https://commondatastorage.googleapis.com/chromium-boringssl-docs/digest.h.html -- for documentation. diff --git a/src/Data/Hmac.chs b/src/Data/Hmac.chs index 907c352..e78c1af 100644 --- a/src/Data/Hmac.chs +++ b/src/Data/Hmac.chs @@ -33,10 +33,10 @@ import Foreign.Marshal.Unsafe (unsafeLocalState) import Unsafe.Coerce (unsafeCoerce) {#import Data.Digest.Internal#} - (Algorithm(Algorithm), Digest(Digest), Engine, EvpMd, evpMaxMdSize, - noEngine) + (Algorithm(Algorithm), Digest(Digest), evpMaxMdSize) import Foreign.Ptr.Cast (asVoidPtr) {#import Foreign.Ptr.ConstantTimeEquals#} (constantTimeEquals) +{#import Internal.Base#} import Result type LazyByteString = ByteString.Lazy.ByteString diff --git a/src/Internal/Base.chs b/src/Internal/Base.chs new file mode 100644 index 0000000..552e76b --- /dev/null +++ b/src/Internal/Base.chs @@ -0,0 +1,38 @@ +-- 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 #-} +{-# 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#} diff --git a/src/Internal/Digest.chs b/src/Internal/Digest.chs new file mode 100644 index 0000000..46b497e --- /dev/null +++ b/src/Internal/Digest.chs @@ -0,0 +1,28 @@ +-- 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 #-} +{-# OPTIONS_GHC -Wno-orphans #-} + +module Internal.Digest where + +import Foreign (Storable(alignment, sizeOf)) + +{#import Internal.Base#} + +#include + +instance Storable EvpMdCtx where + sizeOf _ = {#sizeof EVP_MD_CTX#} + alignment _ = {#alignof EVP_MD_CTX#} -- cgit v1.2.3