{-# OPTIONS_HADDOCK not-home #-}
module Crypto.Encrypt.Public
(
PublicKey
, toPublicKey
, SecretKey
, toSecretKey
, keypair
, keypairFromSeed
, unsafeKeypairFromSeed
, Nonce
, toNonce
, encrypt
, decrypt
) where
import Data.ByteArray (ByteArray, ByteArrayAccess, ScrubbedBytes, withByteArray)
import Data.ByteArray.Sized as Sized (SizedByteArray, alloc, allocRet)
import Data.ByteString (ByteString)
import Data.Functor (void)
import Data.Proxy (Proxy(..))
import System.IO.Unsafe (unsafePerformIO)
import qualified Libsodium as Na
import NaCl.Box
(Nonce, PublicKey, SecretKey, keypair, toNonce, toPublicKey, toSecretKey)
import qualified NaCl.Box as NaCl.Box
encrypt
:: ( ByteArrayAccess pkBytes, ByteArrayAccess skBytes
, ByteArrayAccess nonceBytes
, ByteArrayAccess ptBytes, ByteArray ctBytes
)
=> PublicKey pkBytes
-> SecretKey skBytes
-> Nonce nonceBytes
-> ptBytes
-> ctBytes
encrypt :: PublicKey pkBytes
-> SecretKey skBytes -> Nonce nonceBytes -> ptBytes -> ctBytes
encrypt = PublicKey pkBytes
-> SecretKey skBytes -> Nonce nonceBytes -> ptBytes -> ctBytes
forall pkBytes skBytes nonceBytes ptBytes ctBytes.
(ByteArrayAccess pkBytes, ByteArrayAccess skBytes,
ByteArrayAccess nonceBytes, ByteArrayAccess ptBytes,
ByteArray ctBytes) =>
PublicKey pkBytes
-> SecretKey skBytes -> Nonce nonceBytes -> ptBytes -> ctBytes
NaCl.Box.create
decrypt
:: ( ByteArrayAccess skBytes, ByteArrayAccess pkBytes
, ByteArrayAccess nonceBytes
, ByteArray ptBytes, ByteArrayAccess ctBytes
)
=> SecretKey skBytes
-> PublicKey pkBytes
-> Nonce nonceBytes
-> ctBytes
-> Maybe ptBytes
decrypt :: SecretKey skBytes
-> PublicKey pkBytes
-> Nonce nonceBytes
-> ctBytes
-> Maybe ptBytes
decrypt = SecretKey skBytes
-> PublicKey pkBytes
-> Nonce nonceBytes
-> ctBytes
-> Maybe ptBytes
forall skBytes pkBytes nonceBytes ptBytes ctBytes.
(ByteArrayAccess skBytes, ByteArrayAccess pkBytes,
ByteArrayAccess nonceBytes, ByteArray ptBytes,
ByteArrayAccess ctBytes) =>
SecretKey skBytes
-> PublicKey pkBytes
-> Nonce nonceBytes
-> ctBytes
-> Maybe ptBytes
NaCl.Box.open
type Seed a = SizedByteArray Na.CRYPTO_BOX_SEEDBYTES a
keypairFromSeed
:: ByteArrayAccess seed
=> Seed seed
-> IO (PublicKey ByteString, SecretKey ScrubbedBytes)
keypairFromSeed :: Seed seed -> IO (PublicKey ByteString, SecretKey ScrubbedBytes)
keypairFromSeed Seed seed
seed = do
Proxy 32
-> (Ptr CUChar -> IO (PublicKey ByteString))
-> IO (PublicKey ByteString, SecretKey ScrubbedBytes)
forall (n :: Nat) c p a.
ByteArrayN n c =>
Proxy n -> (Ptr p -> IO a) -> IO (a, c)
allocRet Proxy 32
forall k (t :: k). Proxy t
Proxy ((Ptr CUChar -> IO (PublicKey ByteString))
-> IO (PublicKey ByteString, SecretKey ScrubbedBytes))
-> (Ptr CUChar -> IO (PublicKey ByteString))
-> IO (PublicKey ByteString, SecretKey ScrubbedBytes)
forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
skPtr ->
(Ptr CUChar -> IO ()) -> IO (PublicKey ByteString)
forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
alloc ((Ptr CUChar -> IO ()) -> IO (PublicKey ByteString))
-> (Ptr CUChar -> IO ()) -> IO (PublicKey ByteString)
forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
pkPtr ->
Seed seed -> (Ptr CUChar -> IO ()) -> IO ()
forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray Seed seed
seed ((Ptr CUChar -> IO ()) -> IO ()) -> (Ptr CUChar -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr CUChar
sdPtr ->
IO CInt -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr CUChar -> Ptr CUChar -> Ptr CUChar -> IO CInt
forall k1 k2 k3 (pk :: k1) (sk :: k2) (seed :: k3).
Ptr CUChar -> Ptr CUChar -> Ptr CUChar -> IO CInt
Na.crypto_box_seed_keypair Ptr CUChar
pkPtr Ptr CUChar
skPtr Ptr CUChar
sdPtr
unsafeKeypairFromSeed
:: ByteArrayAccess seed
=> Seed seed
-> (PublicKey ByteString, SecretKey ScrubbedBytes)
unsafeKeypairFromSeed :: Seed seed -> (PublicKey ByteString, SecretKey ScrubbedBytes)
unsafeKeypairFromSeed = IO (PublicKey ByteString, SecretKey ScrubbedBytes)
-> (PublicKey ByteString, SecretKey ScrubbedBytes)
forall a. IO a -> a
unsafePerformIO (IO (PublicKey ByteString, SecretKey ScrubbedBytes)
-> (PublicKey ByteString, SecretKey ScrubbedBytes))
-> (Seed seed
-> IO (PublicKey ByteString, SecretKey ScrubbedBytes))
-> Seed seed
-> (PublicKey ByteString, SecretKey ScrubbedBytes)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Seed seed -> IO (PublicKey ByteString, SecretKey ScrubbedBytes)
forall seed.
ByteArrayAccess seed =>
Seed seed -> IO (PublicKey ByteString, SecretKey ScrubbedBytes)
keypairFromSeed