{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeApplications #-}

module Simplex.Messaging.Crypto.SNTRUP761.Bindings
  ( KEMPublicKey (..),
    KEMSecretKey,
    KEMCiphertext (..),
    KEMSharedKey (..),
    KEMKeyPair,
    sntrup761Keypair,
    sntrup761Enc,
    sntrup761Dec,
  ) where

import Control.Concurrent.STM
import Crypto.Random (ChaChaDRG)
import Data.Aeson (FromJSON (..), ToJSON (..))
import Data.Bifunctor (bimap)
import Data.ByteArray (ScrubbedBytes)
import qualified Data.ByteArray as BA
import Data.ByteString (ByteString)
import Simplex.Messaging.Agent.Store.DB (FromField (..), ToField (..))
import Simplex.Messaging.Crypto.SNTRUP761.Bindings.Defines
import Simplex.Messaging.Crypto.SNTRUP761.Bindings.FFI
import Simplex.Messaging.Crypto.SNTRUP761.Bindings.RNG (rngFuncPtr, withDRG)
import Simplex.Messaging.Encoding
import Simplex.Messaging.Encoding.String

newtype KEMPublicKey = KEMPublicKey ByteString
  deriving (KEMPublicKey -> KEMPublicKey -> Bool
(KEMPublicKey -> KEMPublicKey -> Bool)
-> (KEMPublicKey -> KEMPublicKey -> Bool) -> Eq KEMPublicKey
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: KEMPublicKey -> KEMPublicKey -> Bool
== :: KEMPublicKey -> KEMPublicKey -> Bool
$c/= :: KEMPublicKey -> KEMPublicKey -> Bool
/= :: KEMPublicKey -> KEMPublicKey -> Bool
Eq, Int -> KEMPublicKey -> ShowS
[KEMPublicKey] -> ShowS
KEMPublicKey -> String
(Int -> KEMPublicKey -> ShowS)
-> (KEMPublicKey -> String)
-> ([KEMPublicKey] -> ShowS)
-> Show KEMPublicKey
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> KEMPublicKey -> ShowS
showsPrec :: Int -> KEMPublicKey -> ShowS
$cshow :: KEMPublicKey -> String
show :: KEMPublicKey -> String
$cshowList :: [KEMPublicKey] -> ShowS
showList :: [KEMPublicKey] -> ShowS
Show)

newtype KEMSecretKey = KEMSecretKey ScrubbedBytes
  deriving (KEMSecretKey -> KEMSecretKey -> Bool
(KEMSecretKey -> KEMSecretKey -> Bool)
-> (KEMSecretKey -> KEMSecretKey -> Bool) -> Eq KEMSecretKey
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: KEMSecretKey -> KEMSecretKey -> Bool
== :: KEMSecretKey -> KEMSecretKey -> Bool
$c/= :: KEMSecretKey -> KEMSecretKey -> Bool
/= :: KEMSecretKey -> KEMSecretKey -> Bool
Eq, Int -> KEMSecretKey -> ShowS
[KEMSecretKey] -> ShowS
KEMSecretKey -> String
(Int -> KEMSecretKey -> ShowS)
-> (KEMSecretKey -> String)
-> ([KEMSecretKey] -> ShowS)
-> Show KEMSecretKey
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> KEMSecretKey -> ShowS
showsPrec :: Int -> KEMSecretKey -> ShowS
$cshow :: KEMSecretKey -> String
show :: KEMSecretKey -> String
$cshowList :: [KEMSecretKey] -> ShowS
showList :: [KEMSecretKey] -> ShowS
Show)

newtype KEMCiphertext = KEMCiphertext ByteString
  deriving (KEMCiphertext -> KEMCiphertext -> Bool
(KEMCiphertext -> KEMCiphertext -> Bool)
-> (KEMCiphertext -> KEMCiphertext -> Bool) -> Eq KEMCiphertext
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: KEMCiphertext -> KEMCiphertext -> Bool
== :: KEMCiphertext -> KEMCiphertext -> Bool
$c/= :: KEMCiphertext -> KEMCiphertext -> Bool
/= :: KEMCiphertext -> KEMCiphertext -> Bool
Eq, Int -> KEMCiphertext -> ShowS
[KEMCiphertext] -> ShowS
KEMCiphertext -> String
(Int -> KEMCiphertext -> ShowS)
-> (KEMCiphertext -> String)
-> ([KEMCiphertext] -> ShowS)
-> Show KEMCiphertext
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> KEMCiphertext -> ShowS
showsPrec :: Int -> KEMCiphertext -> ShowS
$cshow :: KEMCiphertext -> String
show :: KEMCiphertext -> String
$cshowList :: [KEMCiphertext] -> ShowS
showList :: [KEMCiphertext] -> ShowS
Show)

newtype KEMSharedKey = KEMSharedKey ScrubbedBytes
  deriving (KEMSharedKey -> KEMSharedKey -> Bool
(KEMSharedKey -> KEMSharedKey -> Bool)
-> (KEMSharedKey -> KEMSharedKey -> Bool) -> Eq KEMSharedKey
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: KEMSharedKey -> KEMSharedKey -> Bool
== :: KEMSharedKey -> KEMSharedKey -> Bool
$c/= :: KEMSharedKey -> KEMSharedKey -> Bool
/= :: KEMSharedKey -> KEMSharedKey -> Bool
Eq, Int -> KEMSharedKey -> ShowS
[KEMSharedKey] -> ShowS
KEMSharedKey -> String
(Int -> KEMSharedKey -> ShowS)
-> (KEMSharedKey -> String)
-> ([KEMSharedKey] -> ShowS)
-> Show KEMSharedKey
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> KEMSharedKey -> ShowS
showsPrec :: Int -> KEMSharedKey -> ShowS
$cshow :: KEMSharedKey -> String
show :: KEMSharedKey -> String
$cshowList :: [KEMSharedKey] -> ShowS
showList :: [KEMSharedKey] -> ShowS
Show)

unsafeRevealKEMSharedKey :: KEMSharedKey -> String
unsafeRevealKEMSharedKey :: KEMSharedKey -> String
unsafeRevealKEMSharedKey (KEMSharedKey ScrubbedBytes
scrubbed) = ByteString -> String
forall a. Show a => a -> String
show (ScrubbedBytes -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert ScrubbedBytes
scrubbed :: ByteString)
{-# DEPRECATED unsafeRevealKEMSharedKey "unsafeRevealKEMSharedKey left in code" #-}

type KEMKeyPair = (KEMPublicKey, KEMSecretKey)

sntrup761Keypair :: TVar ChaChaDRG -> IO KEMKeyPair
sntrup761Keypair :: TVar ChaChaDRG -> IO KEMKeyPair
sntrup761Keypair TVar ChaChaDRG
drg =
  (ByteString -> KEMPublicKey)
-> (ScrubbedBytes -> KEMSecretKey)
-> (ByteString, ScrubbedBytes)
-> KEMKeyPair
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap ByteString -> KEMPublicKey
KEMPublicKey ScrubbedBytes -> KEMSecretKey
KEMSecretKey
    ((ByteString, ScrubbedBytes) -> KEMKeyPair)
-> IO (ByteString, ScrubbedBytes) -> IO KEMKeyPair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int
-> (Ptr Word8 -> IO ByteString) -> IO (ByteString, ScrubbedBytes)
forall ba p a. ByteArray ba => Int -> (Ptr p -> IO a) -> IO (a, ba)
forall p a. Int -> (Ptr p -> IO a) -> IO (a, ScrubbedBytes)
BA.allocRet
      Int
c_SNTRUP761_SECRETKEY_SIZE
      ( \Ptr Word8
skPtr ->
          Int -> (Ptr Word8 -> IO ()) -> IO ByteString
forall ba p. ByteArray ba => Int -> (Ptr p -> IO ()) -> IO ba
BA.alloc Int
c_SNTRUP761_PUBLICKEY_SIZE ((Ptr Word8 -> IO ()) -> IO ByteString)
-> (Ptr Word8 -> IO ()) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
pkPtr ->
            TVar ChaChaDRG -> (Ptr () -> IO ()) -> IO ()
forall a. TVar ChaChaDRG -> (Ptr () -> IO a) -> IO a
withDRG TVar ChaChaDRG
drg ((Ptr () -> IO ()) -> IO ()) -> (Ptr () -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr ()
cxtPtr -> Ptr Word8 -> Ptr Word8 -> Ptr () -> FunPtr RNGFunc -> IO ()
c_sntrup761_keypair Ptr Word8
pkPtr Ptr Word8
skPtr Ptr ()
cxtPtr FunPtr RNGFunc
rngFuncPtr
      )

sntrup761Enc :: TVar ChaChaDRG -> KEMPublicKey -> IO (KEMCiphertext, KEMSharedKey)
sntrup761Enc :: TVar ChaChaDRG -> KEMPublicKey -> IO (KEMCiphertext, KEMSharedKey)
sntrup761Enc TVar ChaChaDRG
drg (KEMPublicKey ByteString
pk) =
  ByteString
-> (Ptr Word8 -> IO (KEMCiphertext, KEMSharedKey))
-> IO (KEMCiphertext, KEMSharedKey)
forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
forall p a. ByteString -> (Ptr p -> IO a) -> IO a
BA.withByteArray ByteString
pk ((Ptr Word8 -> IO (KEMCiphertext, KEMSharedKey))
 -> IO (KEMCiphertext, KEMSharedKey))
-> (Ptr Word8 -> IO (KEMCiphertext, KEMSharedKey))
-> IO (KEMCiphertext, KEMSharedKey)
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
pkPtr ->
    (ByteString -> KEMCiphertext)
-> (ScrubbedBytes -> KEMSharedKey)
-> (ByteString, ScrubbedBytes)
-> (KEMCiphertext, KEMSharedKey)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap ByteString -> KEMCiphertext
KEMCiphertext ScrubbedBytes -> KEMSharedKey
KEMSharedKey
      ((ByteString, ScrubbedBytes) -> (KEMCiphertext, KEMSharedKey))
-> IO (ByteString, ScrubbedBytes)
-> IO (KEMCiphertext, KEMSharedKey)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int
-> (Ptr Word8 -> IO ByteString) -> IO (ByteString, ScrubbedBytes)
forall ba p a. ByteArray ba => Int -> (Ptr p -> IO a) -> IO (a, ba)
forall p a. Int -> (Ptr p -> IO a) -> IO (a, ScrubbedBytes)
BA.allocRet
        Int
c_SNTRUP761_SIZE
        ( \Ptr Word8
kPtr ->
            Int -> (Ptr Word8 -> IO ()) -> IO ByteString
forall ba p. ByteArray ba => Int -> (Ptr p -> IO ()) -> IO ba
BA.alloc Int
c_SNTRUP761_CIPHERTEXT_SIZE ((Ptr Word8 -> IO ()) -> IO ByteString)
-> (Ptr Word8 -> IO ()) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
cPtr ->
              TVar ChaChaDRG -> (Ptr () -> IO ()) -> IO ()
forall a. TVar ChaChaDRG -> (Ptr () -> IO a) -> IO a
withDRG TVar ChaChaDRG
drg ((Ptr () -> IO ()) -> IO ()) -> (Ptr () -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr ()
cxtPtr -> Ptr Word8
-> Ptr Word8 -> Ptr Word8 -> Ptr () -> FunPtr RNGFunc -> IO ()
c_sntrup761_enc Ptr Word8
cPtr Ptr Word8
kPtr Ptr Word8
pkPtr Ptr ()
cxtPtr FunPtr RNGFunc
rngFuncPtr
        )

sntrup761Dec :: KEMCiphertext -> KEMSecretKey -> IO KEMSharedKey
sntrup761Dec :: KEMCiphertext -> KEMSecretKey -> IO KEMSharedKey
sntrup761Dec (KEMCiphertext ByteString
c) (KEMSecretKey ScrubbedBytes
sk) =
  ScrubbedBytes -> (Ptr Word8 -> IO KEMSharedKey) -> IO KEMSharedKey
forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
forall p a. ScrubbedBytes -> (Ptr p -> IO a) -> IO a
BA.withByteArray ScrubbedBytes
sk ((Ptr Word8 -> IO KEMSharedKey) -> IO KEMSharedKey)
-> (Ptr Word8 -> IO KEMSharedKey) -> IO KEMSharedKey
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
skPtr ->
    ByteString -> (Ptr Word8 -> IO KEMSharedKey) -> IO KEMSharedKey
forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
forall p a. ByteString -> (Ptr p -> IO a) -> IO a
BA.withByteArray ByteString
c ((Ptr Word8 -> IO KEMSharedKey) -> IO KEMSharedKey)
-> (Ptr Word8 -> IO KEMSharedKey) -> IO KEMSharedKey
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
cPtr ->
      ScrubbedBytes -> KEMSharedKey
KEMSharedKey
        (ScrubbedBytes -> KEMSharedKey)
-> IO ScrubbedBytes -> IO KEMSharedKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> (Ptr Word8 -> IO ()) -> IO ScrubbedBytes
forall ba p. ByteArray ba => Int -> (Ptr p -> IO ()) -> IO ba
BA.alloc Int
c_SNTRUP761_SIZE (\Ptr Word8
kPtr -> Ptr Word8 -> Ptr Word8 -> Ptr Word8 -> IO ()
c_sntrup761_dec Ptr Word8
kPtr Ptr Word8
cPtr Ptr Word8
skPtr)

instance Encoding KEMSecretKey where
  smpEncode :: KEMSecretKey -> ByteString
smpEncode (KEMSecretKey ScrubbedBytes
c) = Large -> ByteString
forall a. Encoding a => a -> ByteString
smpEncode (Large -> ByteString)
-> (ByteString -> Large) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Large
Large (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ ScrubbedBytes -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert ScrubbedBytes
c
  smpP :: Parser KEMSecretKey
smpP = ScrubbedBytes -> KEMSecretKey
KEMSecretKey (ScrubbedBytes -> KEMSecretKey)
-> (Large -> ScrubbedBytes) -> Large -> KEMSecretKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert (ByteString -> ScrubbedBytes)
-> (Large -> ByteString) -> Large -> ScrubbedBytes
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Large -> ByteString
unLarge (Large -> KEMSecretKey)
-> Parser ByteString Large -> Parser KEMSecretKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ByteString Large
forall a. Encoding a => Parser a
smpP

instance StrEncoding KEMSecretKey where
  strEncode :: KEMSecretKey -> ByteString
strEncode (KEMSecretKey ScrubbedBytes
pk) = ByteString -> ByteString
forall a. StrEncoding a => a -> ByteString
strEncode (ScrubbedBytes -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert ScrubbedBytes
pk :: ByteString)
  strP :: Parser KEMSecretKey
strP = ScrubbedBytes -> KEMSecretKey
KEMSecretKey (ScrubbedBytes -> KEMSecretKey)
-> (ByteString -> ScrubbedBytes) -> ByteString -> KEMSecretKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert (ByteString -> KEMSecretKey)
-> Parser ByteString ByteString -> Parser KEMSecretKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. StrEncoding a => Parser a
strP @ByteString

instance Encoding KEMPublicKey where
  smpEncode :: KEMPublicKey -> ByteString
smpEncode (KEMPublicKey ByteString
pk) = Large -> ByteString
forall a. Encoding a => a -> ByteString
smpEncode (Large -> ByteString)
-> (ByteString -> Large) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Large
Large (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert ByteString
pk
  smpP :: Parser KEMPublicKey
smpP = ByteString -> KEMPublicKey
KEMPublicKey (ByteString -> KEMPublicKey)
-> (Large -> ByteString) -> Large -> KEMPublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert (ByteString -> ByteString)
-> (Large -> ByteString) -> Large -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Large -> ByteString
unLarge (Large -> KEMPublicKey)
-> Parser ByteString Large -> Parser KEMPublicKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ByteString Large
forall a. Encoding a => Parser a
smpP

instance StrEncoding KEMPublicKey where
  strEncode :: KEMPublicKey -> ByteString
strEncode (KEMPublicKey ByteString
pk) = ByteString -> ByteString
forall a. StrEncoding a => a -> ByteString
strEncode (ByteString -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert ByteString
pk :: ByteString)
  strP :: Parser KEMPublicKey
strP = ByteString -> KEMPublicKey
KEMPublicKey (ByteString -> KEMPublicKey)
-> (ByteString -> ByteString) -> ByteString -> KEMPublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert (ByteString -> KEMPublicKey)
-> Parser ByteString ByteString -> Parser KEMPublicKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. StrEncoding a => Parser a
strP @ByteString

instance Encoding KEMCiphertext where
  smpEncode :: KEMCiphertext -> ByteString
smpEncode (KEMCiphertext ByteString
c) = Large -> ByteString
forall a. Encoding a => a -> ByteString
smpEncode (Large -> ByteString)
-> (ByteString -> Large) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Large
Large (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert ByteString
c
  smpP :: Parser KEMCiphertext
smpP = ByteString -> KEMCiphertext
KEMCiphertext (ByteString -> KEMCiphertext)
-> (Large -> ByteString) -> Large -> KEMCiphertext
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert (ByteString -> ByteString)
-> (Large -> ByteString) -> Large -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Large -> ByteString
unLarge (Large -> KEMCiphertext)
-> Parser ByteString Large -> Parser KEMCiphertext
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ByteString Large
forall a. Encoding a => Parser a
smpP

instance Encoding KEMSharedKey where
  smpEncode :: KEMSharedKey -> ByteString
smpEncode (KEMSharedKey ScrubbedBytes
c) = ByteString -> ByteString
forall a. Encoding a => a -> ByteString
smpEncode (ScrubbedBytes -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert ScrubbedBytes
c :: ByteString)
  smpP :: Parser KEMSharedKey
smpP = ScrubbedBytes -> KEMSharedKey
KEMSharedKey (ScrubbedBytes -> KEMSharedKey)
-> (ByteString -> ScrubbedBytes) -> ByteString -> KEMSharedKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert (ByteString -> KEMSharedKey)
-> Parser ByteString ByteString -> Parser KEMSharedKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Encoding a => Parser a
smpP @ByteString

instance StrEncoding KEMCiphertext where
  strEncode :: KEMCiphertext -> ByteString
strEncode (KEMCiphertext ByteString
pk) = ByteString -> ByteString
forall a. StrEncoding a => a -> ByteString
strEncode (ByteString -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert ByteString
pk :: ByteString)
  strP :: Parser KEMCiphertext
strP = ByteString -> KEMCiphertext
KEMCiphertext (ByteString -> KEMCiphertext)
-> (ByteString -> ByteString) -> ByteString -> KEMCiphertext
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert (ByteString -> KEMCiphertext)
-> Parser ByteString ByteString -> Parser KEMCiphertext
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. StrEncoding a => Parser a
strP @ByteString

instance StrEncoding KEMSharedKey where
  strEncode :: KEMSharedKey -> ByteString
strEncode (KEMSharedKey ScrubbedBytes
pk) = ByteString -> ByteString
forall a. StrEncoding a => a -> ByteString
strEncode (ScrubbedBytes -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert ScrubbedBytes
pk :: ByteString)
  strP :: Parser KEMSharedKey
strP = ScrubbedBytes -> KEMSharedKey
KEMSharedKey (ScrubbedBytes -> KEMSharedKey)
-> (ByteString -> ScrubbedBytes) -> ByteString -> KEMSharedKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert (ByteString -> KEMSharedKey)
-> Parser ByteString ByteString -> Parser KEMSharedKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. StrEncoding a => Parser a
strP @ByteString

instance ToJSON KEMSecretKey where
  toJSON :: KEMSecretKey -> Value
toJSON = KEMSecretKey -> Value
forall a. StrEncoding a => a -> Value
strToJSON
  toEncoding :: KEMSecretKey -> Encoding
toEncoding = KEMSecretKey -> Encoding
forall a. StrEncoding a => a -> Encoding
strToJEncoding

instance FromJSON KEMSecretKey where
  parseJSON :: Value -> Parser KEMSecretKey
parseJSON = String -> Value -> Parser KEMSecretKey
forall a. StrEncoding a => String -> Value -> Parser a
strParseJSON String
"KEMSecretKey"

instance ToJSON KEMPublicKey where
  toJSON :: KEMPublicKey -> Value
toJSON = KEMPublicKey -> Value
forall a. StrEncoding a => a -> Value
strToJSON
  toEncoding :: KEMPublicKey -> Encoding
toEncoding = KEMPublicKey -> Encoding
forall a. StrEncoding a => a -> Encoding
strToJEncoding

instance FromJSON KEMPublicKey where
  parseJSON :: Value -> Parser KEMPublicKey
parseJSON = String -> Value -> Parser KEMPublicKey
forall a. StrEncoding a => String -> Value -> Parser a
strParseJSON String
"KEMPublicKey"

instance ToJSON KEMCiphertext where
  toJSON :: KEMCiphertext -> Value
toJSON = KEMCiphertext -> Value
forall a. StrEncoding a => a -> Value
strToJSON
  toEncoding :: KEMCiphertext -> Encoding
toEncoding = KEMCiphertext -> Encoding
forall a. StrEncoding a => a -> Encoding
strToJEncoding

instance FromJSON KEMCiphertext where
  parseJSON :: Value -> Parser KEMCiphertext
parseJSON = String -> Value -> Parser KEMCiphertext
forall a. StrEncoding a => String -> Value -> Parser a
strParseJSON String
"KEMCiphertext"

instance ToField KEMSharedKey where
  toField :: KEMSharedKey -> SQLData
toField (KEMSharedKey ScrubbedBytes
k) = ByteString -> SQLData
forall a. ToField a => a -> SQLData
toField (ScrubbedBytes -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert ScrubbedBytes
k :: ByteString)

instance FromField KEMSharedKey where
#if defined(dbPostgres)
  fromField f dat = KEMSharedKey . BA.convert @ByteString <$> fromField f dat
#else
  fromField :: FieldParser KEMSharedKey
fromField Field
f = ScrubbedBytes -> KEMSharedKey
KEMSharedKey (ScrubbedBytes -> KEMSharedKey)
-> (ByteString -> ScrubbedBytes) -> ByteString -> KEMSharedKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert @ByteString (ByteString -> KEMSharedKey) -> Ok ByteString -> Ok KEMSharedKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FieldParser ByteString
forall a. FromField a => FieldParser a
fromField Field
f
#endif

instance ToJSON KEMSharedKey where
  toJSON :: KEMSharedKey -> Value
toJSON = KEMSharedKey -> Value
forall a. StrEncoding a => a -> Value
strToJSON
  toEncoding :: KEMSharedKey -> Encoding
toEncoding = KEMSharedKey -> Encoding
forall a. StrEncoding a => a -> Encoding
strToJEncoding

instance FromJSON KEMSharedKey where
  parseJSON :: Value -> Parser KEMSharedKey
parseJSON = String -> Value -> Parser KEMSharedKey
forall a. StrEncoding a => String -> Value -> Parser a
strParseJSON String
"KEMSharedKey"