Add Curve_Ed25519

This commit is contained in:
Olivier Chéron 2017-11-04 15:34:12 +01:00
parent 6b3bf37eea
commit c55dd4d27f

View File

@ -17,6 +17,7 @@ module Crypto.ECC
, Curve_P521R1(..) , Curve_P521R1(..)
, Curve_X25519(..) , Curve_X25519(..)
, Curve_X448(..) , Curve_X448(..)
, Curve_Ed25519(..)
, EllipticCurve(..) , EllipticCurve(..)
, EllipticCurveDH(..) , EllipticCurveDH(..)
, EllipticCurveArith(..) , EllipticCurveArith(..)
@ -25,6 +26,7 @@ module Crypto.ECC
) where ) where
import qualified Crypto.PubKey.ECC.P256 as P256 import qualified Crypto.PubKey.ECC.P256 as P256
import qualified Crypto.ECC.Ed25519 as Ed25519
import qualified Crypto.ECC.Simple.Types as Simple import qualified Crypto.ECC.Simple.Types as Simple
import qualified Crypto.ECC.Simple.Prim as Simple import qualified Crypto.ECC.Simple.Prim as Simple
import Crypto.Random import Crypto.Random
@ -225,6 +227,23 @@ instance EllipticCurveDH Curve_X448 where
where secret = X448.dh p s where secret = X448.dh p s
ecdh prx s p = checkNonZeroDH (ecdhRaw prx s p) ecdh prx s p = checkNonZeroDH (ecdhRaw prx s p)
data Curve_Ed25519 = Curve_Ed25519
deriving (Show,Data,Typeable)
instance EllipticCurve Curve_Ed25519 where
type Point Curve_Ed25519 = Ed25519.Point
type Scalar Curve_Ed25519 = Ed25519.Scalar
curveSizeBits _ = 255
curveGenerateScalar _ = Ed25519.scalarGenerate
curveGenerateKeyPair _ = toKeyPair <$> Ed25519.scalarGenerate
where toKeyPair scalar = KeyPair (Ed25519.toPoint scalar) scalar
encodePoint _ point = Ed25519.pointEncode point
decodePoint _ bs = Ed25519.pointDecode bs
instance EllipticCurveArith Curve_Ed25519 where
pointAdd _ a b = Ed25519.pointAdd a b
pointSmul _ s p = Ed25519.pointMul s p
checkNonZeroDH :: SharedSecret -> CryptoFailable SharedSecret checkNonZeroDH :: SharedSecret -> CryptoFailable SharedSecret
checkNonZeroDH s@(SharedSecret b) checkNonZeroDH s@(SharedSecret b)
| B.constAllZero b = CryptoFailed CryptoError_ScalarMultiplicationInvalid | B.constAllZero b = CryptoFailed CryptoError_ScalarMultiplicationInvalid