packages feed

crypton-2.3.1 / Crypto / PubKey / Ed448.hs

{-# LANGUAGE BangPatterns #-}{-# LANGUAGE GeneralizedNewtypeDeriving #-}-- |-- Module      : Crypto.PubKey.Ed448-- License     : BSD-style-- Maintainer  : Olivier Chéron <olivier.cheron@gmail.com>-- Stability   : experimental-- Portability : unknown---- Ed448 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 8032.module Crypto.PubKey.Ed448 (    SecretKey,    PublicKey,    Signature,    -- * Size constants    publicKeySize,    secretKeySize,    signatureSize,    -- * Smart constructors    signature,    publicKey,    secretKey,    -- * Methods    toPublic,    sign,    unsafeSign,    verify,    generateSecretKey,) whereimport Crypto.Debug (DebugShow (..), debugShowBytes)import Data.Wordimport Foreign.C.Typesimport Foreign.Ptrimport Crypto.Errorimport Crypto.Internal.ByteArray (    ByteArrayAccess,    Bytes,    ScrubbedBytes,    withByteArray, )import qualified Crypto.Internal.ByteArray as Bimport Crypto.Internal.Compatimport Crypto.Internal.Importsimport Crypto.Random-- | An Ed448 Secret keynewtype SecretKey = SecretKey ScrubbedBytes    deriving (Show, Eq, ByteArrayAccess, NFData)instance DebugShow SecretKey where    debugShow = debugShowBytes "SecretKey"-- | An Ed448 public keynewtype PublicKey = PublicKey Bytes    deriving (Show, Eq, ByteArrayAccess, NFData)-- | An Ed448 signaturenewtype Signature = Signature Bytes    deriving (Show, Eq, ByteArrayAccess, NFData)-- | Try to build a public key from a bytearraypublicKey :: ByteArrayAccess ba => ba -> CryptoFailable PublicKeypublicKey bs    | B.length bs == publicKeySize =        CryptoPassed $ PublicKey $ B.copyAndFreeze bs (\_ -> return ())    | otherwise =        CryptoFailed $ CryptoError_PublicKeySizeInvalid-- | Try to build a secret key from a bytearraysecretKey :: ByteArrayAccess ba => ba -> CryptoFailable SecretKeysecretKey bs    | B.length bs == secretKeySize = unsafeDoIO $ withByteArray bs initialize    | otherwise =        CryptoFailed CryptoError_SecretKeyStructureInvalid  where    initialize inp = do        valid <- isValidPtr inp        if valid            then (CryptoPassed . SecretKey) <$> B.copy bs (\_ -> return ())            else return $ CryptoFailed CryptoError_SecretKeyStructureInvalid    isValidPtr _ =        return True{-# NOINLINE secretKey #-}-- | Try to build a signature from a bytearraysignature :: ByteArrayAccess ba => ba -> CryptoFailable Signaturesignature bs    | B.length bs == signatureSize =        CryptoPassed $ Signature $ B.copyAndFreeze bs (\_ -> return ())    | otherwise =        CryptoFailed CryptoError_SecretKeyStructureInvalid-- | Create a public key from a secret keytoPublic :: SecretKey -> PublicKeytoPublic (SecretKey sec) = PublicKey    <$> B.allocAndFreeze publicKeySize    $ \result ->        withByteArray sec $ \psec ->            decaf_ed448_derive_public_key result psec{-# NOINLINE toPublic #-}-- | Sign a message using the key pair--   The public key parameter is ignored and its public key--   is generated from the secret key parameter to prevent--   Double Public Key Signing Function Oracle Attack.sign :: ByteArrayAccess ba => SecretKey -> PublicKey -> ba -> Signaturesign secret _public message =    Signature $ B.allocAndFreeze signatureSize $ \sig ->        withByteArray secret $ \sec ->            withByteArray public $ \pub ->                withByteArray message $ \msg ->                    decaf_ed448_sign sig sec pub msg (fromIntegral msgLen) 0 no_context 0  where    !msgLen = B.length message    public = toPublic secret-- | Sign a message using the key pair.  This is old `sign`, which is-- vulnerable to private key compromise if the given public key does-- not correspond to the secret key. This function is provided for-- performance critical applications. To use it safely, applications-- must verify or derive the public key.unsafeSign :: ByteArrayAccess ba => SecretKey -> PublicKey -> ba -> SignatureunsafeSign secret public message =    Signature $ B.allocAndFreeze signatureSize $ \sig ->        withByteArray secret $ \sec ->            withByteArray public $ \pub ->                withByteArray message $ \msg ->                    decaf_ed448_sign sig sec pub msg (fromIntegral msgLen) 0 no_context 0  where    !msgLen = B.length message-- | Verify a messageverify :: ByteArrayAccess ba => PublicKey -> ba -> Signature -> Boolverify public message signatureVal = unsafeDoIO $    withByteArray signatureVal $ \sig ->        withByteArray public $ \pub ->            withByteArray message $ \msg -> do                r <- decaf_ed448_verify sig pub msg (fromIntegral msgLen) 0 no_context 0                return (r /= 0)  where    !msgLen = B.length message-- | Generate a secret keygenerateSecretKey :: MonadRandom m => m SecretKeygenerateSecretKey = SecretKey <$> getRandomBytes secretKeySize-- | A public key is 57 bytespublicKeySize :: IntpublicKeySize = 57-- | A secret key is 57 bytessecretKeySize :: IntsecretKeySize = 57-- | A signature is 114 bytessignatureSize :: IntsignatureSize = 114no_context :: Ptr Word8no_context = nullPtr -- not supported yetforeign import ccall "crypton_decaf_ed448_derive_public_key"    decaf_ed448_derive_public_key        :: Ptr PublicKey -- public key        -> Ptr SecretKey -- secret key        -> IO ()foreign import ccall "crypton_decaf_ed448_sign"    decaf_ed448_sign        :: Ptr Signature -- signature        -> Ptr SecretKey -- secret        -> Ptr PublicKey -- public        -> Ptr Word8 -- message        -> CSize -- message len        -> Word8 -- prehashed        -> Ptr Word8 -- context        -> Word8 -- context len        -> IO ()foreign import ccall "crypton_decaf_ed448_verify"    decaf_ed448_verify        :: Ptr Signature -- signature        -> Ptr PublicKey -- public        -> Ptr Word8 -- message        -> CSize -- message len        -> Word8 -- prehashed        -> Ptr Word8 -- context        -> Word8 -- context len        -> IO CInt