From fadd7434be6bfc11483dc188aa6e4267e01d5c40 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Sat, 1 Sep 2018 15:14:49 -0400 Subject: Consolidate buffer-handling functions --- btls.cabal | 1 - src/BTLS/BoringSSLPatterns.hs | 41 ----------------------------------------- src/BTLS/Buffer.hs | 21 ++++++++++++++++++++- src/Codec/Crypto/HKDF.hs | 3 +-- src/Data/Digest.hs | 2 +- src/Data/HMAC.hs | 2 +- 6 files changed, 23 insertions(+), 47 deletions(-) delete mode 100644 src/BTLS/BoringSSLPatterns.hs diff --git a/btls.cabal b/btls.cabal index c75c448..d0c4442 100644 --- a/btls.cabal +++ b/btls.cabal @@ -79,7 +79,6 @@ library , BTLS.BoringSSL.HMAC , BTLS.BoringSSL.Mem , BTLS.BoringSSL.Rand - , BTLS.BoringSSLPatterns , BTLS.Buffer , BTLS.CreateWithFinalizer , BTLS.Result diff --git a/src/BTLS/BoringSSLPatterns.hs b/src/BTLS/BoringSSLPatterns.hs deleted file mode 100644 index 8d852a1..0000000 --- a/src/BTLS/BoringSSLPatterns.hs +++ /dev/null @@ -1,41 +0,0 @@ --- Copyright 2017 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.BoringSSLPatterns - ( onBufferOfMaxSize - ) where - -import Data.ByteString (ByteString) -import Foreign (Storable(peek), Ptr, alloca, allocaArray) -import Foreign.C.Types - -import BTLS.Buffer (packCUStringLen) - --- | Allocates a buffer, runs a function 'f' to partially fill it, and packs the --- filled data into a 'ByteString'. 'f' must write the size of the filled data, --- in bytes and not including any trailing null, into its second argument. --- --- If 'f' is safe to use under 'unsafeLocalState', this whole function is safe --- to use under 'unsafeLocalState'. -onBufferOfMaxSize :: - (Integral size, Storable size) - => Int - -> (Ptr CUChar -> Ptr size -> IO ()) - -> IO ByteString -onBufferOfMaxSize maxSize f = - allocaArray maxSize $ \pOut -> - alloca $ \pOutLen -> do - f pOut pOutLen - outLen <- peek pOutLen - packCUStringLen (pOut, outLen) diff --git a/src/BTLS/Buffer.hs b/src/BTLS/Buffer.hs index 1a334e9..7168a10 100644 --- a/src/BTLS/Buffer.hs +++ b/src/BTLS/Buffer.hs @@ -15,12 +15,13 @@ module BTLS.Buffer ( unsafeUseAsCBuffer , packCUStringLen + , onBufferOfMaxSize ) where import Data.ByteString (ByteString) import qualified Data.ByteString as ByteString import qualified Data.ByteString.Unsafe as ByteString -import Foreign (Ptr, castPtr) +import Foreign (Storable(peek), Ptr, alloca, allocaArray, castPtr) import Foreign.C.Types unsafeUseAsCBuffer :: ByteString -> ((Ptr a, CULong) -> IO b) -> IO b @@ -31,3 +32,21 @@ unsafeUseAsCBuffer bs f = packCUStringLen :: Integral n => (Ptr CUChar, n) -> IO ByteString packCUStringLen (pStr, len) = ByteString.packCStringLen (castPtr pStr, fromIntegral len) + +-- | Allocates a buffer, runs a function 'f' to partially fill it, and packs the +-- filled data into a 'ByteString'. 'f' must write the size of the filled data, +-- in bytes and not including any trailing null, into its second argument. +-- +-- If 'f' is safe to use under 'unsafeLocalState', this whole function is safe +-- to use under 'unsafeLocalState'. +onBufferOfMaxSize :: + (Integral size, Storable size) + => Int + -> (Ptr CUChar -> Ptr size -> IO ()) + -> IO ByteString +onBufferOfMaxSize maxSize f = + allocaArray maxSize $ \pOut -> + alloca $ \pOutLen -> do + f pOut pOutLen + outLen <- peek pOutLen + packCUStringLen (pOut, outLen) diff --git a/src/Codec/Crypto/HKDF.hs b/src/Codec/Crypto/HKDF.hs index 5dd9b1f..f40f707 100644 --- a/src/Codec/Crypto/HKDF.hs +++ b/src/Codec/Crypto/HKDF.hs @@ -22,8 +22,7 @@ import Foreign.Marshal.Unsafe (unsafeLocalState) import BTLS.BoringSSL.Digest (evpMaxMDSize) import BTLS.BoringSSL.HKDF -import BTLS.BoringSSLPatterns (onBufferOfMaxSize) -import BTLS.Buffer (packCUStringLen) +import BTLS.Buffer (onBufferOfMaxSize, packCUStringLen) import BTLS.Types ( Algorithm(Algorithm), AssociatedData(AssociatedData), Salt(Salt) , SecretKey(SecretKey), noSalt diff --git a/src/Data/Digest.hs b/src/Data/Digest.hs index 2faf66e..336b38d 100644 --- a/src/Data/Digest.hs +++ b/src/Data/Digest.hs @@ -27,7 +27,7 @@ import Foreign.Marshal.Unsafe (unsafeLocalState) import BTLS.BoringSSL.Base import BTLS.BoringSSL.Digest -import BTLS.BoringSSLPatterns (onBufferOfMaxSize) +import BTLS.Buffer (onBufferOfMaxSize) import BTLS.Types (Algorithm(Algorithm), Digest(Digest)) type LazyByteString = ByteString.Lazy.ByteString diff --git a/src/Data/HMAC.hs b/src/Data/HMAC.hs index 812b638..bf1bef8 100644 --- a/src/Data/HMAC.hs +++ b/src/Data/HMAC.hs @@ -30,7 +30,7 @@ import BTLS.BoringSSL.Base import BTLS.BoringSSL.Digest (evpMaxMDSize) import BTLS.BoringSSL.HMAC import BTLS.BoringSSL.Mem (cryptoMemcmp) -import BTLS.BoringSSLPatterns (onBufferOfMaxSize) +import BTLS.Buffer (onBufferOfMaxSize) import BTLS.Result (Result, check) import BTLS.Types (Algorithm(Algorithm), Digest(Digest), SecretKey(SecretKey)) -- cgit v1.2.3