{-# LANGUAGE TypeFamilies #-}
module Crypto.Noise.DH.Curve448
(
Curve448
) where
import Crypto.Error (throwCryptoError, maybeCryptoError)
import Crypto.Random.Entropy (getEntropy)
import qualified Crypto.PubKey.Curve448 as C
import Crypto.Noise.DH
import Data.ByteArray (ScrubbedBytes, convert)
data Curve448
instance DH Curve448 where
newtype PublicKey Curve448 = PK448 C.PublicKey
newtype SecretKey Curve448 = SK448 C.SecretKey
dhName :: forall (proxy :: * -> *). proxy Curve448 -> ScrubbedBytes
dhName proxy Curve448
_ = ScrubbedBytes
"448"
dhLength :: forall (proxy :: * -> *). proxy Curve448 -> Int
dhLength proxy Curve448
_ = Int
56
dhGenKey :: IO (KeyPair Curve448)
dhGenKey = IO (KeyPair Curve448)
genKey
dhPerform :: SecretKey Curve448 -> PublicKey Curve448 -> ScrubbedBytes
dhPerform = SecretKey Curve448 -> PublicKey Curve448 -> ScrubbedBytes
dh
dhPubToBytes :: PublicKey Curve448 -> ScrubbedBytes
dhPubToBytes = PublicKey Curve448 -> ScrubbedBytes
pubToBytes
dhBytesToPub :: ScrubbedBytes -> Maybe (PublicKey Curve448)
dhBytesToPub = ScrubbedBytes -> Maybe (PublicKey Curve448)
bytesToPub
dhSecToBytes :: SecretKey Curve448 -> ScrubbedBytes
dhSecToBytes = SecretKey Curve448 -> ScrubbedBytes
secToBytes
dhBytesToPair :: ScrubbedBytes -> Maybe (KeyPair Curve448)
dhBytesToPair = ScrubbedBytes -> Maybe (KeyPair Curve448)
bytesToPair
dhPubEq :: PublicKey Curve448 -> PublicKey Curve448 -> Bool
dhPubEq = PublicKey Curve448 -> PublicKey Curve448 -> Bool
pubEq
genKey :: IO (KeyPair Curve448)
genKey :: IO (KeyPair Curve448)
genKey = do
ScrubbedBytes
r <- Int -> IO ScrubbedBytes
forall byteArray. ByteArray byteArray => Int -> IO byteArray
getEntropy Int
56 :: IO ScrubbedBytes
let sk :: SecretKey
sk = CryptoFailable SecretKey -> SecretKey
forall a. CryptoFailable a -> a
throwCryptoError (CryptoFailable SecretKey -> SecretKey)
-> (ScrubbedBytes -> CryptoFailable SecretKey)
-> ScrubbedBytes
-> SecretKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScrubbedBytes -> CryptoFailable SecretKey
forall bs. ByteArrayAccess bs => bs -> CryptoFailable SecretKey
C.secretKey (ScrubbedBytes -> SecretKey) -> ScrubbedBytes -> SecretKey
forall a b. (a -> b) -> a -> b
$ ScrubbedBytes
r
pk :: PublicKey
pk = SecretKey -> PublicKey
C.toPublic SecretKey
sk
KeyPair Curve448 -> IO (KeyPair Curve448)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (SecretKey -> SecretKey Curve448
SK448 SecretKey
sk, PublicKey -> PublicKey Curve448
PK448 PublicKey
pk)
dh :: SecretKey Curve448 -> PublicKey Curve448 -> ScrubbedBytes
dh :: SecretKey Curve448 -> PublicKey Curve448 -> ScrubbedBytes
dh (SK448 SecretKey
sk) (PK448 PublicKey
pk) = DhSecret -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert (DhSecret -> ScrubbedBytes) -> DhSecret -> ScrubbedBytes
forall a b. (a -> b) -> a -> b
$ PublicKey -> SecretKey -> DhSecret
C.dh PublicKey
pk SecretKey
sk
pubToBytes :: PublicKey Curve448 -> ScrubbedBytes
pubToBytes :: PublicKey Curve448 -> ScrubbedBytes
pubToBytes (PK448 PublicKey
pk) = PublicKey -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert PublicKey
pk
bytesToPub :: ScrubbedBytes -> Maybe (PublicKey Curve448)
bytesToPub :: ScrubbedBytes -> Maybe (PublicKey Curve448)
bytesToPub = (PublicKey -> PublicKey Curve448)
-> Maybe PublicKey -> Maybe (PublicKey Curve448)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PublicKey -> PublicKey Curve448
PK448 (Maybe PublicKey -> Maybe (PublicKey Curve448))
-> (ScrubbedBytes -> Maybe PublicKey)
-> ScrubbedBytes
-> Maybe (PublicKey Curve448)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CryptoFailable PublicKey -> Maybe PublicKey
forall a. CryptoFailable a -> Maybe a
maybeCryptoError (CryptoFailable PublicKey -> Maybe PublicKey)
-> (ScrubbedBytes -> CryptoFailable PublicKey)
-> ScrubbedBytes
-> Maybe PublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScrubbedBytes -> CryptoFailable PublicKey
forall bs. ByteArrayAccess bs => bs -> CryptoFailable PublicKey
C.publicKey
secToBytes :: SecretKey Curve448 -> ScrubbedBytes
secToBytes :: SecretKey Curve448 -> ScrubbedBytes
secToBytes (SK448 SecretKey
sk) = SecretKey -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert SecretKey
sk
bytesToPair :: ScrubbedBytes -> Maybe (KeyPair Curve448)
bytesToPair :: ScrubbedBytes -> Maybe (KeyPair Curve448)
bytesToPair ScrubbedBytes
bs = do
SecretKey
sk <- CryptoFailable SecretKey -> Maybe SecretKey
forall a. CryptoFailable a -> Maybe a
maybeCryptoError (CryptoFailable SecretKey -> Maybe SecretKey)
-> (ScrubbedBytes -> CryptoFailable SecretKey)
-> ScrubbedBytes
-> Maybe SecretKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScrubbedBytes -> CryptoFailable SecretKey
forall bs. ByteArrayAccess bs => bs -> CryptoFailable SecretKey
C.secretKey (ScrubbedBytes -> Maybe SecretKey)
-> ScrubbedBytes -> Maybe SecretKey
forall a b. (a -> b) -> a -> b
$ ScrubbedBytes
bs
KeyPair Curve448 -> Maybe (KeyPair Curve448)
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return (SecretKey -> SecretKey Curve448
SK448 SecretKey
sk, PublicKey -> PublicKey Curve448
PK448 (SecretKey -> PublicKey
C.toPublic SecretKey
sk))
pubEq :: PublicKey Curve448
-> PublicKey Curve448
-> Bool
pubEq :: PublicKey Curve448 -> PublicKey Curve448 -> Bool
pubEq (PK448 PublicKey
a) (PK448 PublicKey
b) = PublicKey
a PublicKey -> PublicKey -> Bool
forall a. Eq a => a -> a -> Bool
== PublicKey
b