{-# LANGUAGE DeriveDataTypeable, GeneralizedNewtypeDeriving, DeriveGeneric, ForeignFunctionInterface #-}
-- |
-- Module      : Crypto.Saltine.Internal.Box
-- Copyright   : (c) Max Amanshauser 2021
-- License     : MIT
--
-- Maintainer  : max@lambdalifting.org
-- Stability   : experimental
-- Portability : non-portable
--
module Crypto.Saltine.Internal.Box (
    box_publickeybytes
  , box_secretkeybytes
  , box_noncebytes
  , box_zerobytes
  , box_boxzerobytes
  , box_macbytes
  , box_beforenmbytes
  , box_sealbytes
  , c_box_keypair
  , c_box_easy
  , c_box_open_easy
  , c_box_beforenm
  , c_box_easy_afternm
  , c_box_open_easy_afternm
  , c_box_seal
  , c_box_seal_open
  , SecretKey(..)
  , PublicKey(..)
  , Keypair(..)
  , CombinedKey(..)
  , Nonce(..)
) 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

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

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

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

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

-- | A convenience type for keypairs
data Keypair = Keypair {
    Keypair -> SecretKey
secretKey :: SecretKey
  , Keypair -> PublicKey
publicKey :: PublicKey
} deriving (Eq Keypair
Eq Keypair
-> (Keypair -> Keypair -> Ordering)
-> (Keypair -> Keypair -> Bool)
-> (Keypair -> Keypair -> Bool)
-> (Keypair -> Keypair -> Bool)
-> (Keypair -> Keypair -> Bool)
-> (Keypair -> Keypair -> Keypair)
-> (Keypair -> Keypair -> Keypair)
-> Ord Keypair
Keypair -> Keypair -> Bool
Keypair -> Keypair -> Ordering
Keypair -> Keypair -> Keypair
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
$ccompare :: Keypair -> Keypair -> Ordering
compare :: Keypair -> Keypair -> Ordering
$c< :: Keypair -> Keypair -> Bool
< :: Keypair -> Keypair -> Bool
$c<= :: Keypair -> Keypair -> Bool
<= :: Keypair -> Keypair -> Bool
$c> :: Keypair -> Keypair -> Bool
> :: Keypair -> Keypair -> Bool
$c>= :: Keypair -> Keypair -> Bool
>= :: Keypair -> Keypair -> Bool
$cmax :: Keypair -> Keypair -> Keypair
max :: Keypair -> Keypair -> Keypair
$cmin :: Keypair -> Keypair -> Keypair
min :: Keypair -> Keypair -> Keypair
Ord, Typeable Keypair
Typeable Keypair
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Keypair -> c Keypair)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c Keypair)
-> (Keypair -> Constr)
-> (Keypair -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c Keypair))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Keypair))
-> ((forall b. Data b => b -> b) -> Keypair -> Keypair)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Keypair -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Keypair -> r)
-> (forall u. (forall d. Data d => d -> u) -> Keypair -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Keypair -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Keypair -> m Keypair)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Keypair -> m Keypair)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Keypair -> m Keypair)
-> Data Keypair
Keypair -> Constr
Keypair -> DataType
(forall b. Data b => b -> b) -> Keypair -> Keypair
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) -> Keypair -> u
forall u. (forall d. Data d => d -> u) -> Keypair -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Keypair -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Keypair -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Keypair -> m Keypair
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Keypair -> m Keypair
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Keypair
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Keypair -> c Keypair
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Keypair)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Keypair)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Keypair -> c Keypair
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Keypair -> c Keypair
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Keypair
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Keypair
$ctoConstr :: Keypair -> Constr
toConstr :: Keypair -> Constr
$cdataTypeOf :: Keypair -> DataType
dataTypeOf :: Keypair -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Keypair)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Keypair)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Keypair)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Keypair)
$cgmapT :: (forall b. Data b => b -> b) -> Keypair -> Keypair
gmapT :: (forall b. Data b => b -> b) -> Keypair -> Keypair
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Keypair -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Keypair -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Keypair -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Keypair -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Keypair -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> Keypair -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Keypair -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Keypair -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Keypair -> m Keypair
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Keypair -> m Keypair
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Keypair -> m Keypair
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Keypair -> m Keypair
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Keypair -> m Keypair
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Keypair -> m Keypair
Data, Typeable, (forall x. Keypair -> Rep Keypair x)
-> (forall x. Rep Keypair x -> Keypair) -> Generic Keypair
forall x. Rep Keypair x -> Keypair
forall x. Keypair -> Rep Keypair x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Keypair -> Rep Keypair x
from :: forall x. Keypair -> Rep Keypair x
$cto :: forall x. Rep Keypair x -> Keypair
to :: forall x. Rep Keypair x -> Keypair
Generic)

instance Eq Keypair where
    Keypair
kp1 == :: Keypair -> Keypair -> Bool
== Keypair
kp2 = ByteString -> ByteString -> Bool
U.compare (SecretKey -> ByteString
forall a. IsEncoding a => a -> ByteString
encode (SecretKey -> ByteString) -> SecretKey -> ByteString
forall a b. (a -> b) -> a -> b
$ Keypair -> SecretKey
secretKey Keypair
kp1) (SecretKey -> ByteString
forall a. IsEncoding a => a -> ByteString
encode (SecretKey -> ByteString) -> SecretKey -> ByteString
forall a b. (a -> b) -> a -> b
$ Keypair -> SecretKey
secretKey Keypair
kp2)
            Bool -> Bool -> Bool
!&&! ByteString -> ByteString -> Bool
U.compare (PublicKey -> ByteString
forall a. IsEncoding a => a -> ByteString
encode (PublicKey -> ByteString) -> PublicKey -> ByteString
forall a b. (a -> b) -> a -> b
$ Keypair -> PublicKey
publicKey Keypair
kp1) (PublicKey -> ByteString
forall a. IsEncoding a => a -> ByteString
encode (PublicKey -> ByteString) -> PublicKey -> ByteString
forall a b. (a -> b) -> a -> b
$ Keypair -> PublicKey
publicKey Keypair
kp2)

instance Show Keypair where
    show :: Keypair -> String
show Keypair
k = String
"Box.Keypair {secretKey = " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> SecretKey -> String
forall a. Show a => a -> String
show (Keypair -> SecretKey
secretKey Keypair
k) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
", publicKey = " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> PublicKey -> String
forall a. Show a => a -> String
show (Keypair -> PublicKey
publicKey Keypair
k) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"}"

instance Hashable Keypair
instance NFData   Keypair

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

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

-- | An opaque 'box' 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
$c== :: Nonce -> Nonce -> Bool
== :: Nonce -> Nonce -> Bool
$c/= :: Nonce -> Nonce -> Bool
/= :: 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
$ccompare :: Nonce -> Nonce -> Ordering
compare :: Nonce -> Nonce -> Ordering
$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
>= :: Nonce -> Nonce -> Bool
$cmax :: Nonce -> Nonce -> Nonce
max :: Nonce -> Nonce -> Nonce
$cmin :: Nonce -> Nonce -> Nonce
min :: Nonce -> Nonce -> Nonce
Ord, Eq Nonce
Eq Nonce
-> (Int -> Nonce -> Int) -> (Nonce -> Int) -> Hashable Nonce
Int -> Nonce -> Int
Nonce -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
$chashWithSalt :: Int -> Nonce -> Int
hashWithSalt :: Int -> Nonce -> Int
$chash :: Nonce -> Int
hash :: Nonce -> Int
Hashable, Typeable Nonce
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 -> Constr
Nonce -> DataType
(forall b. Data b => b -> b) -> Nonce -> 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)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Nonce -> c Nonce
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Nonce -> c Nonce
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Nonce
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c Nonce
$ctoConstr :: Nonce -> Constr
toConstr :: Nonce -> Constr
$cdataTypeOf :: Nonce -> DataType
dataTypeOf :: Nonce -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Nonce)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c Nonce)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Nonce)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c Nonce)
$cgmapT :: (forall b. Data b => b -> b) -> Nonce -> Nonce
gmapT :: (forall b. Data b => b -> b) -> Nonce -> Nonce
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Nonce -> r
gmapQl :: forall r r'.
(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
gmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Nonce -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> Nonce -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> Nonce -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Nonce -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Nonce -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Nonce -> m Nonce
gmapM :: forall (m :: * -> *).
Monad m =>
(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
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(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
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Nonce -> m 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
$cfrom :: forall x. Nonce -> Rep Nonce x
from :: forall x. Nonce -> Rep Nonce x
$cto :: forall x. Rep Nonce x -> Nonce
to :: forall x. Rep Nonce x -> Nonce
Generic, Nonce -> ()
(Nonce -> ()) -> NFData Nonce
forall a. (a -> ()) -> NFData a
$crnf :: Nonce -> ()
rnf :: Nonce -> ()
NFData)
instance Show Nonce where
    show :: Nonce -> String
show Nonce
k = String
"Box.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
box_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
box_noncebytes Word8
0)
  nudge :: Nonce -> Nonce
nudge (Nonce ByteString
n) = ByteString -> Nonce
Nonce (ByteString -> ByteString
nudgeBS ByteString
n)


box_publickeybytes, box_secretkeybytes, box_noncebytes, box_zerobytes, box_boxzerobytes :: Int
box_macbytes, box_beforenmbytes, box_sealbytes :: Int

-- Box
-- | Size of a @crypto_box@ public key
box_publickeybytes :: Int
box_publickeybytes  = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_box_publickeybytes
-- | Size of a @crypto_box@ secret key
box_secretkeybytes :: Int
box_secretkeybytes  = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_box_secretkeybytes
-- | Size of a @crypto_box@ nonce
box_noncebytes :: Int
box_noncebytes      = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_box_noncebytes
-- | Size of 0-padding prepended to messages before using @crypto_box@
-- or after using @crypto_box_open@
box_zerobytes :: Int
box_zerobytes       = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_box_zerobytes
-- | Size of 0-padding prepended to ciphertext before using
-- @crypto_box_open@ or after using @crypto_box@.
box_boxzerobytes :: Int
box_boxzerobytes    = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_box_boxzerobytes
box_macbytes :: Int
box_macbytes        = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_box_macbytes
-- | Size of a @crypto_box_beforenm@-generated combined key
box_beforenmbytes :: Int
box_beforenmbytes   = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_box_beforenmbytes

-- SealedBox
-- | Amount by which ciphertext is longer than plaintext
-- in sealed boxes
box_sealbytes :: Int
box_sealbytes       = CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
c_crypto_box_sealbytes

-- src/libsodium/crypto_box/crypto_box.c
foreign import ccall "crypto_box_publickeybytes"
  c_crypto_box_publickeybytes :: CSize
foreign import ccall "crypto_box_secretkeybytes"
  c_crypto_box_secretkeybytes :: CSize
foreign import ccall "crypto_box_beforenmbytes"
  c_crypto_box_beforenmbytes :: CSize
foreign import ccall "crypto_box_noncebytes"
  c_crypto_box_noncebytes :: CSize
foreign import ccall "crypto_box_zerobytes"
  c_crypto_box_zerobytes :: CSize
foreign import ccall "crypto_box_boxzerobytes"
  c_crypto_box_boxzerobytes :: CSize
foreign import ccall "crypto_box_macbytes"
  c_crypto_box_macbytes :: CSize

-- src/libsodium/crypto_box_seal.c
foreign import ccall "crypto_box_sealbytes"
  c_crypto_box_sealbytes :: CSize

-- | Should always return a 0.
foreign import ccall "crypto_box_keypair"
  c_box_keypair :: Ptr CChar
                -- ^ Public key
                -> Ptr CChar
                -- ^ Secret key
                -> IO CInt
                -- ^ Always 0

-- | The secretbox C API uses C strings.
foreign import ccall "crypto_box_easy"
  c_box_easy :: Ptr CChar
             -- ^ Cipher output buffer
             -> Ptr CChar
             -- ^ Constant message input buffer
             -> CULLong
             -- ^ Length of message input buffer
             -> Ptr CChar
             -- ^ Constant nonce buffer
             -> Ptr CChar
             -- ^ Constant public key buffer
             -> Ptr CChar
             -- ^ Constant secret key buffer
             -> IO CInt
             -- ^ Always 0

-- | The secretbox C API uses C strings.
foreign import ccall "crypto_box_open_easy"
  c_box_open_easy :: Ptr CChar
                  -- ^ Message output buffer
                  -> Ptr CChar
                  -- ^ Constant ciphertext input buffer
                  -> CULLong
                  -- ^ Length of message input buffer
                  -> Ptr CChar
                  -- ^ Constant nonce buffer
                  -> Ptr CChar
                  -- ^ Constant public key buffer
                  -> Ptr CChar
                  -- ^ Constant secret key buffer
                  -> IO CInt
                  -- ^ 0 for success, -1 for failure to verify

-- | Single target key precompilation.
foreign import ccall "crypto_box_beforenm"
  c_box_beforenm :: Ptr CChar
                 -- ^ Combined key output buffer
                 -> Ptr CChar
                 -- ^ Constant public key buffer
                 -> Ptr CChar
                 -- ^ Constant secret key buffer
                 -> IO CInt
                 -- ^ Always 0

-- | Precompiled key crypto box. Uses C strings.
foreign import ccall "crypto_box_easy_afternm"
  c_box_easy_afternm :: Ptr CChar
                     -- ^ Cipher output buffer
                     -> Ptr CChar
                     -- ^ Constant message input buffer
                     -> CULLong
                     -- ^ Length of message input buffer (incl. 0s)
                     -> Ptr CChar
                     -- ^ Constant nonce buffer
                     -> Ptr CChar
                     -- ^ Constant combined key buffer
                     -> IO CInt
                     -- ^ Always 0

-- | The secretbox C API uses C strings.
foreign import ccall "crypto_box_open_easy_afternm"
  c_box_open_easy_afternm :: Ptr CChar
                          -- ^ Message output buffer
                          -> Ptr CChar
                          -- ^ Constant ciphertext input buffer
                          -> CULLong
                          -- ^ Length of message input buffer (incl. 0s)
                          -> Ptr CChar
                          -- ^ Constant nonce buffer
                          -> Ptr CChar
                          -- ^ Constant combined key buffer
                          -> IO CInt
                          -- ^ 0 for success, -1 for failure to verify


-- | The sealedbox C API uses C strings.
foreign import ccall "crypto_box_seal"
  c_box_seal :: Ptr CChar
             -- ^ Cipher output buffer
             -> Ptr CChar
             -- ^ Constant message input buffer
             -> CULLong
             -- ^ Length of message input buffer
             -> Ptr CChar
             -- ^ Constant public key buffer
             -> IO CInt
             -- ^ Always 0

-- | The sealedbox C API uses C strings.
foreign import ccall "crypto_box_seal_open"
  c_box_seal_open :: Ptr CChar
                  -- ^ Message output buffer
                  -> Ptr CChar
                  -- ^ Constant ciphertext input buffer
                  -> CULLong
                  -- ^ Length of message input buffer
                  -> Ptr CChar
                  -- ^ Constant public key buffer
                  -> Ptr CChar
                  -- ^ Constant secret key buffer
                  -> IO CInt
                  -- ^ 0 for success, -1 for failure to decrypt