diff options
Diffstat (limited to 'src/Data')
-rw-r--r-- | src/Data/Hmac.hs (renamed from src/Data/Hmac.chs) | 67 |
1 files changed, 5 insertions, 62 deletions
diff --git a/src/Data/Hmac.chs b/src/Data/Hmac.hs index 8cbdda7..d1124b6 100644 --- a/src/Data/Hmac.chs +++ b/src/Data/Hmac.hs @@ -12,8 +12,6 @@ -- License for the specific language governing permissions and limitations under -- the License. -{-# OPTIONS_GHC -Wno-missing-methods #-} - module Data.Hmac ( SecretKey(SecretKey) , Hmac @@ -24,73 +22,18 @@ import Data.ByteString (ByteString) import qualified Data.ByteString as ByteString import qualified Data.ByteString.Lazy as ByteString.Lazy import qualified Data.ByteString.Unsafe as ByteString -import Foreign - (FinalizerPtr, ForeignPtr, Ptr, Storable(alignment, peek, sizeOf), - addForeignPtrFinalizer, alloca, allocaArray, mallocForeignPtr, - withForeignPtr) -import Foreign.C.Types +import Foreign (Storable(peek), alloca, allocaArray, withForeignPtr) import Foreign.Marshal.Unsafe (unsafeLocalState) import Unsafe.Coerce (unsafeCoerce) import Data.Digest.Internal (Algorithm(Algorithm), Digest(Digest)) -import Foreign.Ptr.Cast (asVoidPtr) -{#import Foreign.Ptr.ConstantTimeEquals#} (constantTimeEquals) -{#import Internal.Base#} -{#import Internal.Digest#} -import Result +import Foreign.Ptr.ConstantTimeEquals (constantTimeEquals) +import Internal.Base +import Internal.Digest +import Internal.Hmac type LazyByteString = ByteString.Lazy.ByteString -#include <openssl/hmac.h> - --- First, we build basic bindings to the BoringSSL HMAC interface. - --- | The BoringSSL @HMAC_CTX@ type, representing the state of a pending HMAC --- operation. -data HmacCtx -{#pointer *HMAC_CTX as 'Ptr HmacCtx' -> HmacCtx nocode#} - -instance Storable HmacCtx where - sizeOf _ = {#sizeof HMAC_CTX#} - alignment _ = {#alignof HMAC_CTX#} - --- Imported functions from BoringSSL. See --- https://commondatastorage.googleapis.com/chromium-boringssl-docs/hmac.h.html --- for documentation. --- --- Some of these functions return 'CInt' even though they can never fail. Wrap --- them to prevent warnings. - -hmacUpdate :: Ptr HmacCtx -> Ptr CUChar -> CULong -> IO () -hmacUpdate ctx bytes size = - alwaysSucceeds $ {#call HMAC_Update as ^#} ctx bytes size - --- Convert functions that can in fact fail to throw exceptions instead. - -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 - -hmacFinal :: Ptr HmacCtx -> Ptr CUChar -> Ptr CUInt -> IO () -hmacFinal ctx out outSize = - requireSuccess $ {#call HMAC_Final as ^#} ctx out outSize - --- Now we can build a memory-safe allocator. - --- | Memory-safe allocator for 'HmacCtx'. -mallocHmacCtx :: IO (ForeignPtr HmacCtx) -mallocHmacCtx = do - fp <- mallocForeignPtr - withForeignPtr fp {#call HMAC_CTX_init as ^#} - addForeignPtrFinalizer hmacCtxCleanup fp - return fp - -foreign import ccall "&HMAC_CTX_cleanup" - hmacCtxCleanup :: FinalizerPtr HmacCtx - --- Finally, we're ready to actually implement the HMAC interface. - -- | A secret key used as input to a cipher or HMAC. Equality comparisons on -- this type are variable-time. newtype SecretKey = SecretKey ByteString |