-- |
-- Module      : Crypto.PubKey.Curve448
-- License     : BSD-style
-- Maintainer  : John Galt <jgalt@centromere.net>
-- Stability   : experimental
-- Portability : unknown
--
-- Curve448 support
--
-- Internally uses Decaf point compression to omit the cofactor
-- and implementation by Mike Hamburg.  Externally API and
-- data types are compatible with the encoding specified in RFC 7748.
--
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Crypto.PubKey.Curve448
    ( SecretKey
    , PublicKey
    , DhSecret
    -- * Smart constructors
    , dhSecret
    , publicKey
    , secretKey
    -- * Methods
    , 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

-- | A Curve448 Secret key
newtype SecretKey = SecretKey ScrubbedBytes
    deriving (Int -> SecretKey -> ShowS
[SecretKey] -> ShowS
SecretKey -> String
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
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
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 :: forall p. SecretKey -> Ptr p -> IO ()
$ccopyByteArrayToPtr :: forall p. SecretKey -> Ptr p -> IO ()
withByteArray :: forall p a. 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 -> ()
forall a. (a -> ()) -> NFData a
rnf :: SecretKey -> ()
$crnf :: SecretKey -> ()
NFData)

-- | A Curve448 public key
newtype PublicKey = PublicKey Bytes
    deriving (Int -> PublicKey -> ShowS
[PublicKey] -> ShowS
PublicKey -> String
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
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
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 :: forall p. PublicKey -> Ptr p -> IO ()
$ccopyByteArrayToPtr :: forall p. PublicKey -> Ptr p -> IO ()
withByteArray :: forall p a. 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 -> ()
forall a. (a -> ()) -> NFData a
rnf :: PublicKey -> ()
$crnf :: PublicKey -> ()
NFData)

-- | A Curve448 Diffie Hellman secret related to a
-- public key and a secret key.
newtype DhSecret = DhSecret ScrubbedBytes
    deriving (Int -> DhSecret -> ShowS
[DhSecret] -> ShowS
DhSecret -> String
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
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
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 :: forall p. DhSecret -> Ptr p -> IO ()
$ccopyByteArrayToPtr :: forall p. DhSecret -> Ptr p -> IO ()
withByteArray :: forall p a. 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 -> ()
forall a. (a -> ()) -> NFData a
rnf :: DhSecret -> ()
$crnf :: DhSecret -> ()
NFData)

-- | Try to build a public key from a bytearray
publicKey :: ByteArrayAccess bs => bs -> CryptoFailable PublicKey
publicKey :: forall bs. ByteArrayAccess bs => bs -> CryptoFailable PublicKey
publicKey bs
bs
    | forall ba. ByteArrayAccess ba => ba -> Int
B.length bs
bs forall a. Eq a => a -> a -> Bool
== Int
x448_bytes = forall a. a -> CryptoFailable a
CryptoPassed forall a b. (a -> b) -> a -> b
$ Bytes -> PublicKey
PublicKey forall a b. (a -> b) -> a -> b
$ forall bs1 bs2 p.
(ByteArrayAccess bs1, ByteArray bs2) =>
bs1 -> (Ptr p -> IO ()) -> bs2
B.copyAndFreeze bs
bs (\Ptr Any
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ())
    | Bool
otherwise                 = forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_PublicKeySizeInvalid

-- | Try to build a secret key from a bytearray
secretKey :: ByteArrayAccess bs => bs -> CryptoFailable SecretKey
secretKey :: forall bs. ByteArrayAccess bs => bs -> CryptoFailable SecretKey
secretKey bs
bs
    | forall ba. ByteArrayAccess ba => ba -> Int
B.length bs
bs forall a. Eq a => a -> a -> Bool
== Int
x448_bytes = forall a. IO a -> a
unsafeDoIO forall a b. (a -> b) -> a -> b
$
        forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray bs
bs 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 (forall a. a -> CryptoFailable a
CryptoPassed forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScrubbedBytes -> SecretKey
SecretKey) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall bs1 bs2 p.
(ByteArrayAccess bs1, ByteArray bs2) =>
bs1 -> (Ptr p -> IO ()) -> IO bs2
B.copy bs
bs (\Ptr Any
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ())
                else forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_SecretKeyStructureInvalid
    | Bool
otherwise = forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_SecretKeySizeInvalid
  where
        isValidPtr :: Ptr Word8 -> IO Bool
        isValidPtr :: Ptr Word8 -> IO Bool
isValidPtr Ptr Word8
_ =
            forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
{-# NOINLINE secretKey #-}

-- | Create a DhSecret from a bytearray object
dhSecret :: ByteArrayAccess b => b -> CryptoFailable DhSecret
dhSecret :: forall b. ByteArrayAccess b => b -> CryptoFailable DhSecret
dhSecret b
bs
    | forall ba. ByteArrayAccess ba => ba -> Int
B.length b
bs forall a. Eq a => a -> a -> Bool
== Int
x448_bytes = forall a. a -> CryptoFailable a
CryptoPassed forall a b. (a -> b) -> a -> b
$ ScrubbedBytes -> DhSecret
DhSecret forall a b. (a -> b) -> a -> b
$ forall bs1 bs2 p.
(ByteArrayAccess bs1, ByteArray bs2) =>
bs1 -> (Ptr p -> IO ()) -> bs2
B.copyAndFreeze b
bs (\Ptr Any
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ())
    | Bool
otherwise                 = forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
CryptoError_SharedSecretSizeInvalid

-- | Compute the Diffie Hellman secret from a public key and a secret key.
--
-- This implementation may return an all-zero value as it does not check for
-- the condition.
dh :: PublicKey -> SecretKey -> DhSecret
dh :: PublicKey -> SecretKey -> DhSecret
dh (PublicKey Bytes
pub) (SecretKey ScrubbedBytes
sec) = ScrubbedBytes -> DhSecret
DhSecret forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
    forall a p. ByteArray a => Int -> (Ptr p -> IO ()) -> a
B.allocAndFreeze Int
x448_bytes forall a b. (a -> b) -> a -> b
$ \Ptr Word8
result ->
    forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray ScrubbedBytes
sec           forall a b. (a -> b) -> a -> b
$ \Ptr Word8
psec   ->
    forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray Bytes
pub           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 #-}

-- | Create a public key from a secret key
toPublic :: SecretKey -> PublicKey
toPublic :: SecretKey -> PublicKey
toPublic (SecretKey ScrubbedBytes
sec) = Bytes -> PublicKey
PublicKey forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
    forall a p. ByteArray a => Int -> (Ptr p -> IO ()) -> a
B.allocAndFreeze Int
x448_bytes     forall a b. (a -> b) -> a -> b
$ \Ptr Word8
result ->
    forall ba p a. ByteArrayAccess ba => ba -> (Ptr p -> IO a) -> IO a
withByteArray ScrubbedBytes
sec               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 #-}

-- | Generate a secret key.
generateSecretKey :: MonadRandom m => m SecretKey
generateSecretKey :: forall (m :: * -> *). MonadRandom m => m SecretKey
generateSecretKey = ScrubbedBytes -> SecretKey
SecretKey forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) byteArray.
(MonadRandom m, ByteArray byteArray) =>
Int -> m byteArray
getRandomBytes Int
x448_bytes

x448_bytes :: Int
x448_bytes :: Int
x448_bytes = Int
448 forall a. Integral a => a -> a -> a
`quot` Int
8

foreign import ccall "crypton_decaf_x448"
    decaf_x448 :: Ptr Word8 -- ^ public
               -> Ptr Word8 -- ^ basepoint
               -> Ptr Word8 -- ^ secret
               -> IO ()

foreign import ccall "crypton_decaf_x448_derive_public_key"
    decaf_x448_derive_public_key :: Ptr Word8 -- ^ public
                                 -> Ptr Word8 -- ^ secret
                                 -> IO ()