From 1ba4871032a2317648f25c591feb179e8b82dace Mon Sep 17 00:00:00 2001 From: Kazu Yamamoto Date: Mon, 5 Dec 2016 13:34:33 +0900 Subject: [PATCH] fixing P256 binary format. --- Crypto/ECC.hs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Crypto/ECC.hs b/Crypto/ECC.hs index e2897da..40092a0 100644 --- a/Crypto/ECC.hs +++ b/Crypto/ECC.hs @@ -100,8 +100,20 @@ instance EllipticCurve Curve_P256R1 where curveGenerateScalar _ = P256.scalarGenerate curveGenerateKeyPair _ = toKeyPair <$> P256.scalarGenerate where toKeyPair scalar = KeyPair (P256.toPoint scalar) scalar - encodePoint _ p = P256.pointToBinary p - decodePoint _ bs = P256.pointFromBinary bs + encodePoint _ p = mxy + where + mxy :: forall bs. ByteArray bs => bs + mxy = B.concat [uncompressed, xy] + where + uncompressed, xy :: bs + uncompressed = B.singleton 4 + xy = P256.pointToBinary p + decodePoint _ mxy = case B.uncons mxy of + Nothing -> CryptoFailed $ CryptoError_PointSizeInvalid + Just (m,xy) + -- uncompressed + | m == 4 -> P256.pointFromBinary xy + | otherwise -> CryptoFailed $ CryptoError_PointFormatInvalid instance EllipticCurveArith Curve_P256R1 where pointAdd _ a b = P256.pointAdd a b