{-# LANGUAGE GeneralizedNewtypeDeriving #-}{-# LANGUAGE ScopedTypeVariables #-}-- |-- Module : Crypto.PubKey.Curve25519-- License : BSD-style-- Maintainer : Vincent Hanquez <vincent@snarc.org>-- Stability : experimental-- Portability : unknown---- Curve25519 supportmodule Crypto.PubKey.Curve25519 ( SecretKey, PublicKey, DhSecret, -- * Smart constructors dhSecret, publicKey, secretKey, -- * Methods dh, toPublic, generateSecretKey,) whereimport Crypto.Debug (DebugShow (..), debugShowBytes)import Data.Bitsimport Data.Wordimport Foreign.Ptrimport Foreign.Storableimport Crypto.Errorimport Crypto.Internal.ByteArray ( ByteArrayAccess, Bytes, ScrubbedBytes, withByteArray, )import qualified Crypto.Internal.ByteArray as Bimport Crypto.Internal.Compatimport Crypto.Internal.Importsimport Crypto.Random-- | A Curve25519 Secret keynewtype SecretKey = SecretKey ScrubbedBytes deriving (Show, Eq, ByteArrayAccess, NFData)instance DebugShow SecretKey where debugShow = debugShowBytes "SecretKey"-- | A Curve25519 public keynewtype PublicKey = PublicKey Bytes deriving (Show, Eq, ByteArrayAccess, NFData)-- | A Curve25519 Diffie Hellman secret related to a-- public key and a secret key.newtype DhSecret = DhSecret ScrubbedBytes deriving (Show, Eq, ByteArrayAccess, NFData)-- | Try to build a public key from a bytearraypublicKey :: ByteArrayAccess bs => bs -> CryptoFailable PublicKeypublicKey bs | B.length bs == 32 = CryptoPassed $ PublicKey $ B.copyAndFreeze bs (\_ -> return ()) | otherwise = CryptoFailed CryptoError_PublicKeySizeInvalid-- | Try to build a secret key from a bytearraysecretKey :: ByteArrayAccess bs => bs -> CryptoFailable SecretKeysecretKey bs | B.length bs == 32 = unsafeDoIO $ do withByteArray bs $ \inp -> do valid <- isValidPtr inp if valid then (CryptoPassed . SecretKey) <$> B.copy bs (\_ -> return ()) else return $ CryptoFailed CryptoError_SecretKeyStructureInvalid | otherwise = CryptoFailed CryptoError_SecretKeySizeInvalid where -- e[0] &= 0xf8; -- e[31] &= 0x7f; -- e[31] |= 40; isValidPtr :: Ptr Word8 -> IO Bool isValidPtr _ = do -- b0 <- peekElemOff inp 0 -- b31 <- peekElemOff inp 31 return True{- return $ and [ testBit b0 0 == False , testBit b0 1 == False , testBit b0 2 == False , testBit b31 7 == False , testBit b31 6 == True ]-}{-# NOINLINE secretKey #-}-- | Create a DhSecret from a bytearray objectdhSecret :: ByteArrayAccess b => b -> CryptoFailable DhSecretdhSecret bs | B.length bs == 32 = CryptoPassed $ DhSecret $ B.copyAndFreeze bs (\_ -> return ()) | otherwise = CryptoFailed 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 -> DhSecretdh (PublicKey pub) (SecretKey sec) = DhSecret <$> B.allocAndFreeze 32 $ \result -> withByteArray sec $ \psec -> withByteArray pub $ \ppub -> ccrypton_x25519 result psec ppub{-# NOINLINE dh #-}-- | Create a public key from a secret key-- The base point does not go in: where the assembly is built there is a table-- for this, and it is four to five times less work than multiplying the point-- 9 the general way.toPublic :: SecretKey -> PublicKeytoPublic (SecretKey sec) = PublicKey <$> B.allocAndFreeze 32 $ \result -> withByteArray sec $ \psec -> ccrypton_x25519_base result psec{-# NOINLINE toPublic #-}-- | Generate a secret key.generateSecretKey :: MonadRandom m => m SecretKeygenerateSecretKey = tweakToSecretKey <$> getRandomBytes 32 where tweakToSecretKey :: ScrubbedBytes -> SecretKey tweakToSecretKey bin = SecretKey $ B.copyAndFreeze bin $ \inp -> do modifyByte inp 0 (\e0 -> e0 .&. 0xf8) modifyByte inp 31 (\e31 -> (e31 .&. 0x7f) .|. 0x40) modifyByte :: Ptr Word8 -> Int -> (Word8 -> Word8) -> IO () modifyByte p n f = peekByteOff p n >>= pokeByteOff p n . fforeign import ccall "crypton_x25519" ccrypton_x25519 :: Ptr Word8 -- ^ public -> Ptr Word8 -- ^ secret -> Ptr Word8 -- ^ basepoint -> IO ()foreign import ccall "crypton_x25519_base" ccrypton_x25519_base :: Ptr Word8 -- ^ public -> Ptr Word8 -- ^ secret -> IO ()