{-# LANGUAGE DeriveDataTypeable, GeneralizedNewtypeDeriving, DeriveGeneric, ForeignFunctionInterface #-}
-- |
-- Module      : Crypto.Saltine.Internal.SecretBox
-- Copyright   : (c) Max Amanshauser 2021
-- License     : MIT
--
-- Maintainer  : max@lambdalifting.org
-- Stability   : experimental
-- Portability : non-portable
--
module Crypto.Saltine.Internal.SecretBox (
  secretbox_keybytes,
  secretbox_noncebytes,
  secretbox_macbytes,
  secretbox_zerobytes,
  secretbox_boxzerobytes,
  c_secretbox,
  c_secretbox_detached,
  c_secretbox_open,
  c_secretbox_open_detached,
  Key(..),
  Nonce(..),
  Authenticator(..)
) where

import Control.DeepSeq              (NFData)
import Crypto.Saltine.Class
import Crypto.Saltine.Core.Hash     (shorthash)
import Crypto.Saltine.Internal.Hash (nullShKey)
import Crypto.Saltine.Internal.Util as U
import Data.ByteString              (ByteString)
import Data.Data                    (Data, Typeable)
import Data.Hashable                (Hashable)
import Data.Monoid
import Foreign.C
import Foreign.Ptr
import GHC.Generics                 (Generic)

import qualified Data.ByteString as S

secretbox_keybytes, secretbox_noncebytes, secretbox_macbytes, secretbox_zerobytes, secretbox_boxzerobytes :: Int

-- | An opaque 'secretbox' cryptographic key.
newtype Key = Key { Key -> ByteString
unKey :: ByteString } deriving (Eq Key
Eq Key
-> (Key -> Key -> Ordering)
-> (Key -> Key -> Bool)
-> (Key -> Key -> Bool)
-> (Key -> Key -> Bool)
-> (Key -> Key -> Bool)
-> (Key -> Key -> Key)
-> (Key -> Key -> Key)
-> Ord Key
Key -> Key -> Bool
Key -> Key -> Ordering
Key -> Key -> Key
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Key -> Key -> Key
$cmin :: Key -> Key -> Key
max :: Key -> Key -> Key
$cmax :: Key -> Key -> Key
>= :: Key -> Key -> Bool
$c>= :: Key -> Key -> Bool
> :: Key -> Key -> Bool
$c> :: Key -> Key -> Bool
<= :: Key -> Key -> Bool
$c<= :: Key -> Key -> Bool
< :: Key -> Key -> Bool
$c< :: Key -> Key -> Bool
compare :: Key -> Key -> Ordering
$ccompare :: Key -> Key -> Ordering
$cp1Ord :: Eq Key
Ord, Int -> Key -> Int
Key -> Int
(Int -> Key -> Int) -> (Key -> Int) -> Hashable Key
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: Key -> Int
$chash :: Key -> Int
hashWithSalt :: Int -> Key -> Int
$chashWithSalt :: Int -> Key -> Int
Hashable, Typeable Key
DataType
Constr
Typeable Key
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Key -> c Key)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c Key)
-> (Key -> Constr)
-> (Key -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c Key))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Key))
-> ((forall b. Data b => b -> b) -> Key -> Key)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Key -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Key -> r)
-> (forall u. (forall d. Data d => d -> u) -> Key -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Key -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Key -> m Key)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Key -> m Key)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Key -> m Key)
-> Data Key
Key -> DataType
Key -> Constr
(forall b. Data b => b -> b) -> Key -> Key
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Key -> c Key
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Key
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Key -> u
forall u. (forall d. Data d => d -> u) -> Key -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Key -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Key -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Key -> m Key
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Key -> m Key
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Key
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Key -> c Key
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Key)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Key)
$cKey :: Constr
$tKey :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> Key -> m Key
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Key -> m Key
gmapMp :: (forall d. Data d => d -> m d) -> Key -> m Key
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Key -> m Key
gmapM :: (forall d. Data d => d -> m d) -> Key -> m Key
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Key -> m Key
gmapQi :: Int -> (forall d. Data d => d -> u) -> Key -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Key -> u
gmapQ :: (forall d. Data d => d -> u) -> Key -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Key -> [u]
gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Key -> r
$cgmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Key -> r
gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Key -> r
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Key -> r
gmapT :: (forall b. Data b => b -> b) -> Key -> Key
$cgmapT :: (forall b. Data b => b -> b) -> Key -> Key
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Key)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Key)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c Key)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Key)
dataTypeOf :: Key -> DataType
$cdataTypeOf :: Key -> DataType
toConstr :: Key -> Constr
$ctoConstr :: Key -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Key
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Key
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Key -> c Key
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Key -> c Key
$cp1Data :: Typeable Key
Data, Typeable, (forall x. Key -> Rep Key x)
-> (forall x. Rep Key x -> Key) -> Generic Key
forall x. Rep Key x -> Key
forall x. Key -> Rep Key x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Key x -> Key
$cfrom :: forall x. Key -> Rep Key x
Generic, Key -> ()
(Key -> ()) -> NFData Key
forall a. (a -> ()) -> NFData a
rnf :: Key -> ()
$crnf :: Key -> ()
NFData)
instance Eq Key where
    Key ByteString
a == :: Key -> Key -> Bool
== Key ByteString
b = ByteString -> ByteString -> Bool
U.compare ByteString
a ByteString
b
instance Show Key where
    show :: Key -> String
show Key
k = String
"SecretBox.Key {hashesTo = \"" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> (ByteString -> String
bin2hex (ByteString -> String)
-> (ByteString -> ByteString) -> ByteString -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShorthashKey -> ByteString -> ByteString
shorthash ShorthashKey
nullShKey (ByteString -> String) -> ByteString -> String
forall a b. (a -> b) -> a -> b
$ Key -> ByteString
forall a. IsEncoding a => a -> ByteString
encode Key
k) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"}\""

instance IsEncoding Key where
  decode :: ByteString -> Maybe Key
decode ByteString
v = if ByteString -> Int
S.length ByteString
v Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
secretbox_keybytes
           then Key -> Maybe Key
forall a. a -> Maybe a
Just (ByteString -> Key
Key ByteString
v)
           else Maybe Key
forall a. Maybe a
Nothing
  {-# INLINE decode #-}
  encode :: Key -> ByteString
encode (Key ByteString
v) = ByteString
v
  {-# INLINE encode #-}

-- | An opaque 'secretbox' nonce.
newtype Nonce = Nonce { Nonce -> ByteString
unNonce :: ByteString } deriving (Nonce -> Nonce -> Bool
(Nonce -> Nonce -> Bool) -> (Nonce -> Nonce -> Bool) -> Eq Nonce
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Nonce -> Nonce -> Bool
$c/= :: Nonce -> Nonce -> Bool
== :: Nonce -> Nonce -> Bool
$c== :: Nonce -> Nonce -> Bool
Eq, Eq Nonce
Eq Nonce
-> (Nonce -> Nonce -> Ordering)
-> (Nonce -> Nonce -> Bool)
-> (Nonce -> Nonce -> Bool)
-> (Nonce -> Nonce -> Bool)
-> (Nonce -> Nonce -> Bool)
-> (Nonce -> Nonce -> Nonce)
-> (Nonce -> Nonce -> Nonce)
-> Ord Nonce
Nonce -> Nonce -> Bool
Nonce -> Nonce -> Ordering
Nonce -> Nonce -> Nonce
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Nonce -> Nonce -> Nonce
$cmin :: Nonce -> Nonce -> Nonce
max :: Nonce -> Nonce -> Nonce
$cmax :: Nonce -> Nonce -> Nonce
>= :: Nonce -> Nonce -> Bool
$c>= :: Nonce -> Nonce -> Bool
> :: Nonce -> Nonce -> Bool
$c> :: Nonce -> Nonce -> Bool
<= :: Nonce -> Nonce -> Bool
$c<= :: Nonce -> Nonce -> Bool
< :: Nonce -> Nonce -> Bool
$c< :: Nonce -> Nonce -> Bool
compare :: Nonce -> Nonce -> Ordering
$ccompare :: Nonce -> Nonce -> Ordering
$cp1Ord :: Eq Nonce
Ord, Int -> Nonce -> Int
Nonce -> Int
(Int -> Nonce -> Int) -> (Nonce -> Int) -> Hashable Nonce
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: Nonce -> Int
$chash :: Nonce -> Int
hashWithSalt :: Int -> Nonce -> Int
$chashWithSalt :: Int -> Nonce -> Int
Hashable, Typeable Nonce
DataType
Constr
Typeable Nonce
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Nonce -> c Nonce)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c Nonce)
-> (Nonce -> Constr)
-> (Nonce -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c Nonce))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Nonce))
-> ((forall b. Data b => b -> b) -> Nonce -> Nonce)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Nonce -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Nonce -> r)
-> (forall u. (forall d. Data d => d -> u) -> Nonce -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Nonce -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Nonce -> m Nonce)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Nonce -> m Nonce)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Nonce -> m Nonce)
-> Data Nonce
Nonce -> DataType
Nonce -> Constr
(forall b. Data b => b -> b) -> Nonce -> Nonce
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Nonce -> c Nonce
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Nonce
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Nonce -> u
forall u. (forall d. Data d => d -> u) -> Nonce -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Nonce -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Nonce -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Nonce -> m Nonce
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Nonce -> m Nonce
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Nonce
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Nonce -> c Nonce
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Nonce)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Nonce)
$cNonce :: Constr
$tNonce :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> Nonce -> m Nonce
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Nonce -> m Nonce
gmapMp :: (forall d. Data d => d -> m d) -> Nonce -> m Nonce
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Nonce -> m Nonce
gmapM :: (forall d. Data d => d -> m d) -> Nonce -> m Nonce
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Nonce -> m Nonce
gmapQi :: Int -> (forall d. Data d => d -> u) -> Nonce -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Nonce -> u
gmapQ :: (forall d. Data d => d -> u) -> Nonce -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Nonce -> [u]
gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Nonce -> r
$cgmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Nonce -> r
gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Nonce -> r
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Nonce -> r
gmapT :: (forall b. Data b => b -> b) -> Nonce -> Nonce
$cgmapT :: (forall b. Data b => b -> b) -> Nonce -> Nonce
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Nonce)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Nonce)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c Nonce)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Nonce)
dataTypeOf :: Nonce -> DataType
$cdataTypeOf :: Nonce -> DataType
toConstr :: Nonce -> Constr
$ctoConstr :: Nonce -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Nonce
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Nonce
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Nonce -> c Nonce
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Nonce -> c Nonce
$cp1Data :: Typeable Nonce
Data, Typeable, (forall x. Nonce -> Rep Nonce x)
-> (forall x. Rep Nonce x -> Nonce) -> Generic Nonce
forall x. Rep Nonce x -> Nonce
forall x. Nonce -> Rep Nonce x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Nonce x -> Nonce
$cfrom :: forall x. Nonce -> Rep Nonce x
Generic, Nonce -> ()
(Nonce -> ()) -> NFData Nonce
forall a. (a -> ()) -> NFData a
rnf :: Nonce -> ()
$crnf :: Nonce -> ()
NFData)
instance Show Nonce where
    show :: Nonce -> String
show Nonce
k = String
"SecretBox.Nonce " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
bin2hex (Nonce -> ByteString
forall a. IsEncoding a => a -> ByteString
encode Nonce
k)

instance IsEncoding Nonce where
  decode :: ByteString -> Maybe Nonce
decode ByteString
v = if ByteString -> Int
S.length ByteString
v Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
secretbox_noncebytes
           then Nonce -> Maybe Nonce
forall a. a -> Maybe a
Just (ByteString -> Nonce
Nonce ByteString
v)
           else Maybe Nonce
forall a. Maybe a
Nothing
  {-# INLINE decode #-}
  encode :: Nonce -> ByteString
encode (Nonce ByteString
v) = ByteString
v
  {-# INLINE encode #-}

instance IsNonce Nonce where
  zero :: Nonce
zero            = ByteString -> Nonce
Nonce (Int -> Word8 -> ByteString
S.replicate Int
secretbox_noncebytes Word8
0)
  nudge :: Nonce -> Nonce
nudge (Nonce ByteString
n) = ByteString -> Nonce
Nonce (ByteString -> ByteString
nudgeBS ByteString
n)


-- | An Authenticator for a Message
newtype Authenticator = Au { Authenticator -> ByteString
unAu :: ByteString } deriving (Authenticator -> Authenticator -> Bool
(Authenticator -> Authenticator -> Bool)
-> (Authenticator -> Authenticator -> Bool) -> Eq Authenticator
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Authenticator -> Authenticator -> Bool
$c/= :: Authenticator -> Authenticator -> Bool
== :: Authenticator -> Authenticator -> Bool
$c== :: Authenticator -> Authenticator -> Bool
Eq, Eq Authenticator
Eq Authenticator
-> (Authenticator -> Authenticator -> Ordering)
-> (Authenticator -> Authenticator -> Bool)
-> (Authenticator -> Authenticator -> Bool)
-> (Authenticator -> Authenticator -> Bool)
-> (Authenticator -> Authenticator -> Bool)
-> (Authenticator -> Authenticator -> Authenticator)
-> (Authenticator -> Authenticator -> Authenticator)
-> Ord Authenticator
Authenticator -> Authenticator -> Bool
Authenticator -> Authenticator -> Ordering
Authenticator -> Authenticator -> Authenticator
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Authenticator -> Authenticator -> Authenticator
$cmin :: Authenticator -> Authenticator -> Authenticator
max :: Authenticator -> Authenticator -> Authenticator
$cmax :: Authenticator -> Authenticator -> Authenticator
>= :: Authenticator -> Authenticator -> Bool
$c>= :: Authenticator -> Authenticator -> Bool
> :: Authenticator -> Authenticator -> Bool
$c> :: Authenticator -> Authenticator -> Bool
<= :: Authenticator -> Authenticator -> Bool
$c<= :: Authenticator -> Authenticator -> Bool
< :: Authenticator -> Authenticator -> Bool
$c< :: Authenticator -> Authenticator -> Bool
compare :: Authenticator -> Authenticator -> Ordering
$ccompare :: Authenticator -> Authenticator -> Ordering
$cp1Ord :: Eq Authenticator
Ord, Typeable Authenticator
DataType
Constr
Typeable Authenticator
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Authenticator -> c Authenticator)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c Authenticator)
-> (Authenticator -> Constr)
-> (Authenticator -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c Authenticator))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c Authenticator))
-> ((forall b. Data b => b -> b) -> Authenticator -> Authenticator)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Authenticator -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Authenticator -> r)
-> (forall u. (forall d. Data d => d -> u) -> Authenticator -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> Authenticator -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Authenticator -> m Authenticator)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Authenticator -> m Authenticator)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Authenticator -> m Authenticator)
-> Data Authenticator
Authenticator -> DataType
Authenticator -> Constr
(forall b. Data b => b -> b) -> Authenticator -> Authenticator
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Authenticator -> c Authenticator
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Authenticator
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Authenticator -> u
forall u. (forall d. Data d => d -> u) -> Authenticator -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Authenticator -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Authenticator -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Authenticator -> m Authenticator
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Authenticator -> m Authenticator
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Authenticator
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Authenticator -> c Authenticator
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Authenticator)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Authenticator)
$cAu :: Constr
$tAuthenticator :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> Authenticator -> m Authenticator
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Authenticator -> m Authenticator
gmapMp :: (forall d. Data d => d -> m d) -> Authenticator -> m Authenticator
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Authenticator -> m Authenticator
gmapM :: (forall d. Data d => d -> m d) -> Authenticator -> m Authenticator
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Authenticator -> m Authenticator
gmapQi :: Int -> (forall d. Data d => d -> u) -> Authenticator -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Authenticator -> u
gmapQ :: (forall d. Data d => d -> u) -> Authenticator -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Authenticator -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Authenticator -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Authenticator -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Authenticator -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Authenticator -> r
gmapT :: (forall b. Data b => b -> b) -> Authenticator -> Authenticator
$cgmapT :: (forall b. Data b => b -> b) -> Authenticator -> Authenticator
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Authenticator)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c Authenticator)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c Authenticator)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Authenticator)
dataTypeOf :: Authenticator -> DataType
$cdataTypeOf :: Authenticator -> DataType
toConstr :: Authenticator -> Constr
$ctoConstr :: Authenticator -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Authenticator
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Authenticator
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Authenticator -> c Authenticator
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Authenticator -> c Authenticator
$cp1Data :: Typeable Authenticator
Data, Typeable, Int -> Authenticator -> Int
Authenticator -> Int
(Int -> Authenticator -> Int)
-> (Authenticator -> Int) -> Hashable Authenticator
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: Authenticator -> Int
$chash :: Authenticator -> Int
hashWithSalt :: Int -> Authenticator -> Int
$chashWithSalt :: Int -> Authenticator -> Int
Hashable, (forall x. Authenticator -> Rep Authenticator x)
-> (forall x. Rep Authenticator x -> Authenticator)
-> Generic Authenticator
forall x. Rep Authenticator x -> Authenticator
forall x. Authenticator -> Rep Authenticator x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Authenticator x -> Authenticator
$cfrom :: forall x. Authenticator -> Rep Authenticator x
Generic, Authenticator -> ()
(Authenticator -> ()) -> NFData Authenticator
forall a. (a -> ()) -> NFData a
rnf :: Authenticator -> ()
$crnf :: Authenticator -> ()
NFData)
instance Show Authenticator where
    show :: Authenticator -> String
show Authenticator
k = String
"Sign.Authenticator " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ByteString -> String
bin2hex (Authenticator -> ByteString
forall a. IsEncoding a => a -> ByteString
encode Authenticator
k)

instance IsEncoding Authenticator where
  decode :: ByteString -> Maybe Authenticator
decode ByteString
v = if ByteString -> Int
S.length ByteString
v Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
secretbox_macbytes
           then Authenticator -> Maybe Authenticator
forall a. a -> Maybe a
Just (ByteString -> Authenticator
Au ByteString
v)
           else Maybe Authenticator
forall a. Maybe a
Nothing
  {-# INLINE decode #-}
  encode :: Authenticator -> ByteString
encode (Au ByteString
v) = ByteString
v
  {-# INLINE encode #-}


-- | Size of a @crypto_secretbox@ secret key
secretbox_keybytes :: Int
secretbox_keybytes     = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_secretbox_keybytes
-- | Size of a @crypto_secretbox@ nonce
secretbox_noncebytes :: Int
secretbox_noncebytes   = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_secretbox_noncebytes
-- | Size of a @crypto_secretbox@ mac
secretbox_macbytes :: Int
secretbox_macbytes     = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_secretbox_macbytes
-- | Size of 0-padding prepended to messages before using
-- @crypto_secretbox@ or after using @crypto_secretbox_open@
secretbox_zerobytes :: Int
secretbox_zerobytes    = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_secretbox_zerobytes
-- | Size of 0-padding prepended to ciphertext before using
-- @crypto_secretbox_open@ or after using @crypto_secretbox@
secretbox_boxzerobytes :: Int
secretbox_boxzerobytes = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_secretbox_boxzerobytes

-- src/libsodium/crypto_secretbox/crypto_secretbox.c
foreign import ccall "crypto_secretbox_keybytes"
  c_crypto_secretbox_keybytes :: CSize
foreign import ccall "crypto_secretbox_noncebytes"
  c_crypto_secretbox_noncebytes :: CSize
foreign import ccall "crypto_secretbox_macbytes"
  c_crypto_secretbox_macbytes :: CSize
foreign import ccall "crypto_secretbox_zerobytes"
  c_crypto_secretbox_zerobytes :: CSize
foreign import ccall "crypto_secretbox_boxzerobytes"
  c_crypto_secretbox_boxzerobytes :: CSize

-- | The secretbox C API uses 0-padded C strings. Always returns 0.
foreign import ccall "crypto_secretbox"
  c_secretbox
    :: Ptr CChar
    -- ^ Cipher 0-padded output buffer
    -> Ptr CChar
    -- ^ Constant 0-padded message input buffer
    -> CULLong
    -- ^ Length of message input buffer (incl. 0s)
    -> Ptr CChar
    -- ^ Constant nonce buffer
    -> Ptr CChar
    -- ^ Constant key buffer
    -> IO CInt

-- | The secretbox_detached C API uses C strings. Always returns 0.
foreign import ccall "crypto_secretbox_detached"
  c_secretbox_detached
    :: Ptr CChar
    -- ^ Ciphertext output buffer
    -> Ptr CChar
    -- ^ Authentication tag output buffer
    -> Ptr CChar
    -- ^ Constant message input buffer
    -> CULLong
    -- ^ Length of message input buffer (incl. 0s)
    -> Ptr CChar
    -- ^ Constant nonce buffer
    -> Ptr CChar
    -- ^ Constant key buffer
    -> IO CInt

-- | The secretbox C API uses 0-padded C strings. Returns 0 if
-- successful or -1 if verification failed.
foreign import ccall "crypto_secretbox_open"
  c_secretbox_open
    :: Ptr CChar
    -- ^ Message 0-padded output buffer
    -> Ptr CChar
    -- ^ Constant 0-padded message input buffer
    -> CULLong
    -- ^ Length of message input buffer (incl. 0s)
    -> Ptr CChar
    -- ^ Constant nonce buffer
    -> Ptr CChar
    -- ^ Constant key buffer
    -> IO CInt

-- | The secretbox C API uses C strings. Returns 0 if
-- successful or -1 if verification failed.
foreign import ccall "crypto_secretbox_open_detached"
  c_secretbox_open_detached
    :: Ptr CChar
    -- ^ Message output buffer
    -> Ptr CChar
    -- ^ Constant ciphertext input buffer
    -> Ptr CChar
    -- ^ Constant auth tag input buffer
    -> CULLong
    -- ^ Length of ciphertext input buffer
    -> Ptr CChar
    -- ^ Constant nonce buffer
    -> Ptr CChar
    -- ^ Constant key buffer
    -> IO CInt