packages feed

quic-0.3.13: Network/QUIC/Crypto/Types.hs

module Network.QUIC.Crypto.Types (
    -- * Types
    PlainText,
    CipherText,
    Key (..),
    IV (..),
    CID,
    Secret (..),
    AssDat (..),
    Sample (..),
    Mask (..),
    WithMask,
    Nonce (..),
    Salt,
    Label (..),
    Cipher,
    InitialSecret,
    TrafficSecrets,
    ClientTrafficSecret (..),
    ServerTrafficSecret (..),
) where

import Data.ByteArray (ScrubbedBytes, convert)
import qualified Data.ByteString.Char8 as C8
import Network.TLS hiding (Version)
import Network.TLS.QUIC

import Network.QUIC.Imports
import Network.QUIC.Types

----------------------------------------------------------------

type PlainText = ByteString
type CipherText = ByteString
type Salt = ByteString

newtype Key = Key ScrubbedBytes deriving (Eq)
newtype IV = IV ByteString deriving (Eq)
newtype Secret = Secret ScrubbedBytes deriving (Eq)
newtype AssDat = AssDat ByteString deriving (Eq)
newtype Sample = Sample ByteString deriving (Eq)

-- | Running an action on the header protection mask, for as long as the
--   action takes and no longer.
--
-- The mask lives in a buffer the encryption side owns, and the only way to
-- keep the garbage collector from taking that buffer out from under the
-- reader was to allocate it with 'mallocBytes' and never free it -- a leak
-- of 16 or 32 bytes for every coder, which is three to five per connection.
-- Handing the reader in instead of handing the buffer out lets the buffer
-- be a 'ForeignPtr' held across exactly the use, and the collector frees it
-- with the coder.
--
-- 'False' when there is no mask to be had, which is a protector without
-- keys.
type WithMask = (Buffer -> IO ()) -> IO Bool

newtype Mask = Mask ByteString deriving (Eq)
newtype Label = Label ByteString deriving (Eq)
newtype Nonce = Nonce ByteString deriving (Eq)

instance Show Key where
    show (Key x) = "Key=" ++ C8.unpack (enc16 $ convert x)
instance Show IV where
    show (IV x) = "IV=" ++ C8.unpack (enc16 x)
instance Show Secret where
    show (Secret x) = "Secret=" ++ C8.unpack (enc16 $ convert x)
instance Show AssDat where
    show (AssDat x) = "AssDat=" ++ C8.unpack (enc16 x)
instance Show Sample where
    show (Sample x) = "Sample=" ++ C8.unpack (enc16 x)
instance Show Mask where
    show (Mask x) = "Mask=" ++ C8.unpack (enc16 x)
instance Show Label where
    show (Label x) = "Label=" ++ C8.unpack (enc16 x)
instance Show Nonce where
    show (Nonce x) = "Nonce=" ++ C8.unpack (enc16 x)

data InitialSecret