From 44a6b5738f73b5281c3630c507aeece75e6ee952 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Thu, 2 Aug 2018 17:19:33 -0400 Subject: Factor out common char* ↔ unsigned char* cast MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BTLS/Cast.hs | 4 ++++ src/Codec/Crypto/HKDF.hs | 8 ++------ src/Data/Digest.hs | 7 +------ src/Data/HMAC.hs | 7 +------ 4 files changed, 8 insertions(+), 18 deletions(-) diff --git a/src/BTLS/Cast.hs b/src/BTLS/Cast.hs index 6f29469..a467c90 100644 --- a/src/BTLS/Cast.hs +++ b/src/BTLS/Cast.hs @@ -15,7 +15,11 @@ module BTLS.Cast where import Foreign (Ptr) +import Foreign.C.Types import Unsafe.Coerce (unsafeCoerce) +asCUCharBuf :: Ptr CChar -> Ptr CUChar +asCUCharBuf = unsafeCoerce + asVoidPtr :: Ptr a -> Ptr () asVoidPtr = unsafeCoerce diff --git a/src/Codec/Crypto/HKDF.hs b/src/Codec/Crypto/HKDF.hs index 8c1db0a..4547a52 100644 --- a/src/Codec/Crypto/HKDF.hs +++ b/src/Codec/Crypto/HKDF.hs @@ -19,13 +19,12 @@ module Codec.Crypto.HKDF import qualified Data.ByteString as ByteString import qualified Data.ByteString.Unsafe as ByteString -import Foreign (Ptr, Storable(peek), alloca, allocaArray) -import Foreign.C.Types +import Foreign (Storable(peek), alloca, allocaArray) import Foreign.Marshal.Unsafe (unsafeLocalState) -import Unsafe.Coerce (unsafeCoerce) import BTLS.BoringSSL.Digest (evpMaxMDSize) import BTLS.BoringSSL.HKDF +import BTLS.Cast (asCUCharBuf) import BTLS.Types (AssociatedData(AssociatedData), Salt(Salt), SecretKey(SecretKey), noSalt) import Data.Digest.Internal (Algorithm(Algorithm)) @@ -63,6 +62,3 @@ expand (Algorithm md) (AssociatedData info) outLen (SecretKey secret) = (asCUCharBuf pSecret) (fromIntegral secretLen) (asCUCharBuf pInfo) (fromIntegral infoLen) SecretKey <$> ByteString.packCStringLen (pOutKey, outLen) - -asCUCharBuf :: Ptr CChar -> Ptr CUChar -asCUCharBuf = unsafeCoerce diff --git a/src/Data/Digest.hs b/src/Data/Digest.hs index 2d06389..03026fb 100644 --- a/src/Data/Digest.hs +++ b/src/Data/Digest.hs @@ -22,13 +22,11 @@ module Data.Digest ) where import qualified Data.ByteString.Lazy as ByteString.Lazy -import Foreign (Ptr) -import Foreign.C.Types import Foreign.Marshal.Unsafe (unsafeLocalState) -import Unsafe.Coerce (unsafeCoerce) import BTLS.BoringSSL.Base import BTLS.BoringSSL.Digest +import BTLS.Cast (asCUCharBuf) import Data.Digest.Internal type LazyByteString = ByteString.Lazy.ByteString @@ -56,6 +54,3 @@ hash (Algorithm md) = -- we're going to cheat and let Haskell reinterpret-cast 'mdOut' to 'Ptr -- CUChar. evpDigestFinalEx ctx (asCUCharBuf mdOut) pOutSize - - asCUCharBuf :: Ptr CChar -> Ptr CUChar - asCUCharBuf = unsafeCoerce diff --git a/src/Data/HMAC.hs b/src/Data/HMAC.hs index 29b6ce0..a6b72fe 100644 --- a/src/Data/HMAC.hs +++ b/src/Data/HMAC.hs @@ -21,13 +21,11 @@ module Data.HMAC import Data.ByteString (ByteString) import qualified Data.ByteString.Lazy as ByteString.Lazy import qualified Data.ByteString.Unsafe as ByteString -import Foreign (Ptr) -import Foreign.C.Types import Foreign.Marshal.Unsafe (unsafeLocalState) -import Unsafe.Coerce (unsafeCoerce) import BTLS.BoringSSL.Base import BTLS.BoringSSL.HMAC +import BTLS.Cast (asCUCharBuf) import BTLS.ConstantTimeEquals (constantTimeEquals) import BTLS.Types (SecretKey(SecretKey)) import Data.Digest.Internal @@ -65,6 +63,3 @@ hmac (Algorithm md) (SecretKey key) = -- in Data.Digest, we'll let Haskell reinterpret-cast the buffers. update ctx buf len = hmacUpdate ctx (asCUCharBuf buf) len finalize ctx hmacOut pOutSize = hmacFinal ctx (asCUCharBuf hmacOut) pOutSize - - asCUCharBuf :: Ptr CChar -> Ptr CUChar - asCUCharBuf = unsafeCoerce -- cgit v1.2.3