Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jobs:
id: setup-haskell
uses: haskell-actions/setup@v2
with:
ghc-version: '9.8.2'
ghc-version: '9.10.1'
cabal-version: 'latest'

- name: Freeze
Expand Down
2 changes: 2 additions & 0 deletions sel/sel.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ library
Sel.HMAC.SHA256
Sel.HMAC.SHA512
Sel.HMAC.SHA512_256
Sel.KeyMaterialDecodeError
Sel.PublicKey.Cipher
Sel.PublicKey.Seal
Sel.PublicKey.Signature
Expand All @@ -66,6 +67,7 @@ library
Sel.Internal
Sel.Internal.Scoped
Sel.Internal.Scoped.Foreign
Sel.PublicKey.Internal.Signature

build-depends:
, base >=4.14 && <5
Expand Down
8 changes: 5 additions & 3 deletions sel/src/Sel/Hashing/Short.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import qualified Data.Text.Lazy.Builder as Builder
import Foreign hiding (void)
import Foreign.C (CSize, CUChar, CULLong)
import Foreign.C (CChar, CSize, CUChar, CULLong)
import GHC.Exception (Exception)
import GHC.IO.Handle.Text (memcpy)
import System.IO.Unsafe (unsafeDupablePerformIO)

import qualified Data.Base16.Types as Base16
Expand Down Expand Up @@ -250,7 +249,10 @@ binaryToShortHashKey binaryKey =
BS.unsafeUseAsCString binaryKey $ \cString -> do
shortHashKeyFPtr <- Foreign.mallocForeignPtrBytes (fromIntegral cryptoShortHashSipHashX24KeyBytes)
Foreign.withForeignPtr shortHashKeyFPtr $ \shortHashKeyPtr ->
memcpy shortHashKeyPtr (Foreign.castPtr cString) cryptoShortHashSipHashX24KeyBytes
Foreign.copyBytes
shortHashKeyPtr
(Foreign.castPtr @CChar @CUChar cString)
(fromIntegral cryptoShortHashSipHashX24KeyBytes)
pure $ Just $ ShortHashKey shortHashKeyFPtr

-- | Convert a strict hexadecimal-encoded 'Text' to a 'ShortHashKey'.
Expand Down
91 changes: 91 additions & 0 deletions sel/src/Sel/KeyMaterialDecodeError.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE ViewPatterns #-}

-- |
-- Module : Sel.KeyMaterialDecodeError
-- Description : Key material utilities
-- Copyright : (c) Jack Henahan, 2024
-- License : BSD-3-Clause
-- Maintainer : The Haskell Cryptography Group
-- Portability : GHC only
module Sel.KeyMaterialDecodeError
( -- * Key material utilities
KeyMaterialDecodeError (..)
, RequiredLength (..)
, InputLength (..)
, validKeyMaterial
)
where

import Control.Exception (Exception)
import Data.Bifunctor (first)
import Data.ByteString (StrictByteString)
import Data.ByteString qualified as ByteString
import Data.ByteString.Base16 qualified as Base16
import Data.Coerce (coerce)
import Data.Text (Text)
import Data.Text.Display (Display, ShowInstance (..))
import Foreign.C (CSize (..))

-- | Errors arising from decoding key material from bytes.
--
-- @since 0.0.3.0
data KeyMaterialDecodeError
= -- | Input length does not match the length required for the target pointer.
--
-- @since 0.0.3.0
ByteLengthMismatch RequiredLength InputLength
| -- | Input bytes did not decode to hexadecimal.
--
-- @since 0.0.3.0
DecodingFailure Text
deriving stock
( Show
-- ^ @since 0.0.3.0
, Eq
-- ^ @since 0.0.3.0
)
deriving
( Display
-- ^ @since 0.0.3.0
)
via (ShowInstance KeyMaterialDecodeError)
deriving anyclass
( Exception
-- ^ @since 0.0.3.0
)

-- | The length of the target pointer for some key material.
--
-- @since 0.0.3.0
newtype RequiredLength = RequiredLength Int
deriving stock
( Show
-- ^ @since 0.0.3.0
, Eq
-- ^ @since 0.0.3.0
)

-- | The length of some input bytes.
--
-- @since 0.0.3.0
newtype InputLength = InputLength Int
deriving stock
( Show
-- ^ @since 0.0.3.0
, Eq
-- ^ @since 0.0.3.0
)

-- | Attempt to decode a hexadecimal-encoded 'StrictByteString' with an expected length.
--
-- @since 0.0.3.0
validKeyMaterial :: CSize -> StrictByteString -> Either KeyMaterialDecodeError StrictByteString
validKeyMaterial (fromIntegral -> requiredLength) bytes = do
decoded@(ByteString.length -> inputLength) <-
first DecodingFailure (Base16.decodeBase16Untyped bytes)
if requiredLength == inputLength
then Right decoded
else Left $ ByteLengthMismatch (coerce requiredLength) (coerce inputLength)
5 changes: 2 additions & 3 deletions sel/src/Sel/PublicKey/Cipher.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ import Foreign (ForeignPtr, Ptr)
import qualified Foreign
import Foreign.C (CChar, CSize, CUChar, CULLong)
import qualified Foreign.C as Foreign
import GHC.IO.Handle.Text (memcpy)
import System.IO.Unsafe (unsafeDupablePerformIO)

import Control.Exception
Expand Down Expand Up @@ -496,10 +495,10 @@ decrypt
(-1) -> pure Nothing
_ -> do
bsPtr <- Foreign.mallocBytes (fromIntegral messageLength)
memcpy bsPtr (Foreign.castPtr messagePtr) (fromIntegral messageLength)
Foreign.copyBytes bsPtr messagePtr (fromIntegral messageLength)
Just
<$> BS.unsafePackMallocCStringLen
(Foreign.castPtr @CChar bsPtr, fromIntegral messageLength)
(Foreign.castPtr @CUChar @CChar bsPtr, fromIntegral messageLength)

-- | Exception thrown upon error during the generation of
-- the key pair by 'newKeyPair'.
Expand Down
Loading
Loading