{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Crypto.PubKey.Curve448
( SecretKey
, PublicKey
, DhSecret
, dhSecret
, publicKey
, secretKey
, dh
, toPublic
, generateSecretKey
) where
import Data.Word
import Foreign.Ptr
import Crypto.Error
import Crypto.Random
import Crypto.Internal.Compat
import Crypto.Internal.Imports
import Crypto.Internal.ByteArray (ByteArrayAccess, ScrubbedBytes, Bytes, withByteArray)
import qualified Crypto.Internal.ByteArray as B
newtype SecretKey = SecretKey ScrubbedBytes
deriving (Int -> SecretKey -> ShowS
[SecretKey] -> ShowS
SecretKey -> String
(Int -> SecretKey -> ShowS)
-> (SecretKey -> String)
-> ([SecretKey] -> ShowS)
-> Show SecretKey
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SecretKey] -> ShowS
$cshowList :: [SecretKey] -> ShowS
show :: SecretKey -> String
$cshow :: SecretKey -> String
showsPrec :: Int -> SecretKey -> ShowS
$cshowsPrec :: Int -> SecretKey -> ShowS
Show,SecretKey -> SecretKey -> Bool
(SecretKey -> SecretKey -> Bool)
-> (SecretKey -> SecretKey -> Bool) -> Eq SecretKey
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SecretKey -> SecretKey -> Bool
$c/= :: SecretKey -> SecretKey -> Bool
== :: SecretKey -> SecretKey -> Bool
$c== :: SecretKey -> SecretKey -> Bool
Eq,SecretKey -> Int
SecretKey -> Ptr p -> IO ()
SecretKey -> (Ptr p -> IO a) -> IO a
(SecretKey -> Int)
-> (forall p a. SecretKey -> (Ptr p -> IO a) -> IO a)
-> (forall p. SecretKey -> Ptr p -> IO ())
-> ByteArrayAccess SecretKey
forall p. SecretKey -> Ptr p -> IO ()
forall ba.
(ba -> Int)
-> (forall p a. ba -> (Ptr p -> IO a) -> IO a)
-> (forall p. ba -> Ptr p -> IO ())
-> ByteArrayAccess ba
forall p a. SecretKey -> (Ptr p -> IO a) -> IO a
copyByteArrayToPtr :: SecretKey -> Ptr p -> IO ()
$ccopyByteArrayToPtr :: forall p. SecretKey -> Ptr p -> IO ()
withByteArray :: SecretKey -> (Ptr p -> IO a) -> IO a
$cwithByteArray :: forall p a. SecretKey -> (Ptr p -> IO a) -> IO a
length :: SecretKey -> Int
$clength :: SecretKey -> Int
ByteArrayAccess,SecretKey -> ()
(SecretKey -> ()) -> NFData SecretKey
forall a. (a -> ()) -> NFData a
rnf :: SecretKey -> ()
$crnf :: SecretKey -> ()
NFData)
newtype PublicKey = PublicKey Bytes
deriving (Int -> PublicKey -> ShowS
[PublicKey] -> ShowS
PublicKey -> String
(Int -> PublicKey -> ShowS)
-> (PublicKey -> String)
-> ([PublicKey] -> ShowS)
-> Show PublicKey
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PublicKey] -> ShowS
$cshowList :: [PublicKey] -> ShowS
show :: PublicKey -> String
$cshow :: PublicKey -> String
showsPrec :: Int -> PublicKey -> ShowS
$cshowsPrec :: Int -> PublicKey -> ShowS
Show,PublicKey -> PublicKey -> Bool
(PublicKey -> PublicKey -> Bool)
-> (PublicKey -> PublicKey -> Bool) -> Eq PublicKey
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PublicKey -> PublicKey -> Bool
$c/= :: PublicKey -> PublicKey -> Bool
== :: PublicKey -> PublicKey -> Bool
$c== :: PublicKey -> PublicKey -> Bool
Eq,PublicKey -> Int
PublicKey -> Ptr p -> IO ()
PublicKey -> (Ptr p -> IO a) -> IO a
(PublicKey -> Int)
-> (forall p a. PublicKey -> (Ptr p -> IO a) -> IO a)
-> (forall p. PublicKey -> Ptr p -> IO ())
-> ByteArrayAccess PublicKey
forall p. PublicKey -> Ptr p -> IO ()
forall ba.
(ba -> Int)
-> (forall p a. ba -> (Ptr p -> IO a) -> IO a)
-> (forall p. ba -> Ptr p -> IO ())
-> ByteArrayAccess ba
forall p a. PublicKey -> (Ptr p -> IO a) -> IO a
copyByteArrayToPtr :: PublicKey -> Ptr p -> IO ()
$ccopyByteArrayToPtr :: forall p. PublicKey -> Ptr p -> IO ()
withByteArray :: PublicKey -> (Ptr p -> IO a) -> IO a
$cwithByteArray :: forall p a. PublicKey -> (Ptr p -> IO a) -> IO a
length :: PublicKey -> Int
$clength :: PublicKey -> Int
ByteArrayAccess,PublicKey -> ()
(PublicKey -> ()) -> NFData PublicKey
forall a. (a -> ()) -> NFData a
rnf :: PublicKey -> ()
$crnf :: PublicKey -> ()
NFData)
newtype DhSecret = DhSecret ScrubbedBytes
deriving (Int -> DhSecret -> ShowS
[DhSecret] -> ShowS
DhSecret -> String
(Int -> DhSecret -> ShowS)
-> (DhSecret -> String) -> ([DhSecret] -> ShowS) -> Show DhSecret
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DhSecret] -> ShowS
$cshowList :: [DhSecret] -> ShowS
show :: DhSecret -> String
$cshow :: DhSecret -> String
showsPrec :: Int -> DhSecret -> ShowS
$cshowsPrec :: Int -> DhSecret -> ShowS
Show,DhSecret -> DhSecret -> Bool
(DhSecret -> DhSecret -> Bool)
-> (DhSecret -> DhSecret -> Bool) -> Eq DhSecret
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DhSecret -> DhSecret -> Bool
$c/= :: DhSecret -> DhSecret -> Bool
== :: DhSecret -> DhSecret -> Bool
$c== :: DhSecret -> DhSecret -> Bool
Eq,DhSecret -> Int
DhSecret -> Ptr p -> IO ()
DhSecret -> (Ptr p -> IO a) -> IO a
(DhSecret -> Int)
-> (forall p a. DhSecret -> (Ptr p -> IO a) -> IO a)
-> (forall p. DhSecret -> Ptr p -> IO ())
-> ByteArrayAccess DhSecret
forall p. DhSecret -> Ptr p -> IO ()
forall ba.
(ba -> Int)
-> (forall p a. ba -> (Ptr p -> IO a) -> IO a)
-> (forall p. ba -> Ptr p -> IO ())
-> ByteArrayAccess ba
forall p a. DhSecret -> (Ptr p -> IO a) -> IO a
copyByteArrayToPtr :: DhSecret -> Ptr p -> IO ()
$ccopyByteArrayToPtr :: forall p. DhSecret -> Ptr p -> IO ()
withByteArray :: DhSecret -> (Ptr p -> IO a) -> IO a
$cwithByteArray :: forall p a. DhSecret -> (Ptr p -> IO a) -> IO a
length :: DhSecret -> Int
$clength :: DhSecret -> Int
ByteArrayAccess,DhSecret -> ()
(DhSecret -> ()) -> NFData DhSecret
forall a. (a -> ()) -> NFData a
rnf :: DhSecret -> ()
$crnf :: DhSecret -> ()
NFData)
publicKey :: ByteArrayAccess bs => bs -> CryptoFailable PublicKey
publicKey :: bs -> CryptoFailable PublicKey
publicKey bs
bs
| bs -> Int
forall ba. ByteArrayAccess ba => ba -> Int
B.length bs
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
x448_bytes = PublicKey -> CryptoFailable PublicKey
forall a. a -> CryptoFailable a
CryptoPassed (PublicKey -> CryptoFailable PublicKey)
-> PublicKey -> CryptoFailable PublicKey
forall a b. (a -> b) -> a -> b
$ Bytes -> PublicKey
PublicKey (Bytes -> PublicKey) -> Bytes -> PublicKey
forall a b. (a -> b) -> a -> b
$ bs -> (Ptr Any -> IO ()) -> Bytes
forall bs1 bs2 p.
(ByteArrayAccess bs1, ByteArray bs2) =>
bs1 -> (Ptr p -> IO ()) -> bs2
B.copyAndFreeze bs
bs (\Ptr Any
_ -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
| Bool
otherwise = CryptoError -> CryptoFailable PublicKey
forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_PublicKeySizeInvalid
secretKey :: ByteArrayAccess bs => bs -> CryptoFailable SecretKey
secretKey :: bs -> CryptoFailable SecretKey
secretKey bs
bs
| bs -> Int
forall ba. ByteArrayAccess ba => ba -> Int
B.length bs
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
x448_bytes = IO (CryptoFailable SecretKey) -> CryptoFailable SecretKey
forall a. IO a -> a
unsafeDoIO (IO (CryptoFailable SecretKey) -> CryptoFailable SecretKey)
-> IO (CryptoFailable SecretKey) -> CryptoFailable SecretKey
forall a b. (a -> b) -> a -> b
$
bs
-> (Ptr Word8 -> IO (CryptoFailable SecretKey))
-> IO (CryptoFailable SecretKey)
forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray bs
bs ((Ptr Word8 -> IO (CryptoFailable SecretKey))
-> IO (CryptoFailable SecretKey))
-> (Ptr Word8 -> IO (CryptoFailable SecretKey))
-> IO (CryptoFailable SecretKey)
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
inp -> do
Bool
valid <- Ptr Word8 -> IO Bool
isValidPtr Ptr Word8
inp
if Bool
valid
then (SecretKey -> CryptoFailable SecretKey
forall a. a -> CryptoFailable a
CryptoPassed (SecretKey -> CryptoFailable SecretKey)
-> (ScrubbedBytes -> SecretKey)
-> ScrubbedBytes
-> CryptoFailable SecretKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScrubbedBytes -> SecretKey
SecretKey) (ScrubbedBytes -> CryptoFailable SecretKey)
-> IO ScrubbedBytes -> IO (CryptoFailable SecretKey)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> bs -> (Ptr Any -> IO ()) -> IO ScrubbedBytes
forall bs1 bs2 p.
(ByteArrayAccess bs1, ByteArray bs2) =>
bs1 -> (Ptr p -> IO ()) -> IO bs2
B.copy bs
bs (\Ptr Any
_ -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
else CryptoFailable SecretKey -> IO (CryptoFailable SecretKey)
forall (m :: * -> *) a. Monad m => a -> m a
return (CryptoFailable SecretKey -> IO (CryptoFailable SecretKey))
-> CryptoFailable SecretKey -> IO (CryptoFailable SecretKey)
forall a b. (a -> b) -> a -> b
$ CryptoError -> CryptoFailable SecretKey
forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_SecretKeyStructureInvalid
| Bool
otherwise = CryptoError -> CryptoFailable SecretKey
forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_SecretKeySizeInvalid
where
isValidPtr :: Ptr Word8 -> IO Bool
isValidPtr :: Ptr Word8 -> IO Bool
isValidPtr Ptr Word8
_ =
Bool -> IO Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
{-# NOINLINE secretKey #-}
dhSecret :: ByteArrayAccess b => b -> CryptoFailable DhSecret
dhSecret :: b -> CryptoFailable DhSecret
dhSecret b
bs
| b -> Int
forall ba. ByteArrayAccess ba => ba -> Int
B.length b
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
x448_bytes = DhSecret -> CryptoFailable DhSecret
forall a. a -> CryptoFailable a
CryptoPassed (DhSecret -> CryptoFailable DhSecret)
-> DhSecret -> CryptoFailable DhSecret
forall a b. (a -> b) -> a -> b
$ ScrubbedBytes -> DhSecret
DhSecret (ScrubbedBytes -> DhSecret) -> ScrubbedBytes -> DhSecret
forall a b. (a -> b) -> a -> b
$ b -> (Ptr Any -> IO ()) -> ScrubbedBytes
forall bs1 bs2 p.
(ByteArrayAccess bs1, ByteArray bs2) =>
bs1 -> (Ptr p -> IO ()) -> bs2
B.copyAndFreeze b
bs (\Ptr Any
_ -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
| Bool
otherwise = CryptoError -> CryptoFailable DhSecret
forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_SharedSecretSizeInvalid
dh :: PublicKey -> SecretKey -> DhSecret
dh :: PublicKey -> SecretKey -> DhSecret
dh (PublicKey Bytes
pub) (SecretKey ScrubbedBytes
sec) = ScrubbedBytes -> DhSecret
DhSecret (ScrubbedBytes -> DhSecret)
-> ((Ptr Word8 -> IO ()) -> ScrubbedBytes)
-> (Ptr Word8 -> IO ())
-> DhSecret
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
Int -> (Ptr Word8 -> IO ()) -> ScrubbedBytes
forall a p. ByteArray a => Int -> (Ptr p -> IO ()) -> a
B.allocAndFreeze Int
x448_bytes ((Ptr Word8 -> IO ()) -> DhSecret)
-> (Ptr Word8 -> IO ()) -> DhSecret
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
result ->
ScrubbedBytes -> (Ptr Word8 -> IO ()) -> IO ()
forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray ScrubbedBytes
sec ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
psec ->
Bytes -> (Ptr Word8 -> IO ()) -> IO ()
forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray Bytes
pub ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ppub ->
Ptr Word8 -> Ptr Word8 -> Ptr Word8 -> IO ()
decaf_x448 Ptr Word8
result Ptr Word8
ppub Ptr Word8
psec
{-# NOINLINE dh #-}
toPublic :: SecretKey -> PublicKey
toPublic :: SecretKey -> PublicKey
toPublic (SecretKey ScrubbedBytes
sec) = Bytes -> PublicKey
PublicKey (Bytes -> PublicKey)
-> ((Ptr Word8 -> IO ()) -> Bytes)
-> (Ptr Word8 -> IO ())
-> PublicKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
Int -> (Ptr Word8 -> IO ()) -> Bytes
forall a p. ByteArray a => Int -> (Ptr p -> IO ()) -> a
B.allocAndFreeze Int
x448_bytes ((Ptr Word8 -> IO ()) -> PublicKey)
-> (Ptr Word8 -> IO ()) -> PublicKey
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
result ->
ScrubbedBytes -> (Ptr Word8 -> IO ()) -> IO ()
forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray ScrubbedBytes
sec ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
psec ->
Ptr Word8 -> Ptr Word8 -> IO ()
decaf_x448_derive_public_key Ptr Word8
result Ptr Word8
psec
{-# NOINLINE toPublic #-}
generateSecretKey :: MonadRandom m => m SecretKey
generateSecretKey :: m SecretKey
generateSecretKey = ScrubbedBytes -> SecretKey
SecretKey (ScrubbedBytes -> SecretKey) -> m ScrubbedBytes -> m SecretKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> m ScrubbedBytes
forall (m :: * -> *) byteArray.
(MonadRandom m, ByteArray byteArray) =>
Int -> m byteArray
getRandomBytes Int
x448_bytes
x448_bytes :: Int
x448_bytes :: Int
x448_bytes = Int
448 Int -> Int -> Int
forall a. Integral a => a -> a -> a
`quot` Int
8
foreign import ccall "cryptonite_decaf_x448"
decaf_x448 :: Ptr Word8
-> Ptr Word8
-> Ptr Word8
-> IO ()
foreign import ccall "cryptonite_decaf_x448_derive_public_key"
decaf_x448_derive_public_key :: Ptr Word8
-> Ptr Word8
-> IO ()