From c55dd4d27f0b6a87222df3a05d40071ac1c2222f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Ch=C3=A9ron?= Date: Sat, 4 Nov 2017 15:34:12 +0100 Subject: [PATCH] Add Curve_Ed25519 --- Crypto/ECC.hs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Crypto/ECC.hs b/Crypto/ECC.hs index 576247d..54f56dd 100644 --- a/Crypto/ECC.hs +++ b/Crypto/ECC.hs @@ -17,6 +17,7 @@ module Crypto.ECC , Curve_P521R1(..) , Curve_X25519(..) , Curve_X448(..) + , Curve_Ed25519(..) , EllipticCurve(..) , EllipticCurveDH(..) , EllipticCurveArith(..) @@ -25,6 +26,7 @@ module Crypto.ECC ) where 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.Prim as Simple import Crypto.Random @@ -225,6 +227,23 @@ instance EllipticCurveDH Curve_X448 where where secret = X448.dh p s 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 s@(SharedSecret b) | B.constAllZero b = CryptoFailed CryptoError_ScalarMultiplicationInvalid