Validate P256 point when decoding

Fixes #165.
This commit is contained in:
Olivier Chéron 2017-06-01 20:58:53 +02:00
parent b3b2e86b53
commit 5c2988716e

View File

@ -114,7 +114,7 @@ instance EllipticCurve Curve_P256R1 where
Nothing -> CryptoFailed $ CryptoError_PointSizeInvalid
Just (m,xy)
-- uncompressed
| m == 4 -> P256.pointFromBinary xy
| m == 4 -> P256.pointFromBinary xy >>= validateP256Point
| otherwise -> CryptoFailed $ CryptoError_PointFormatInvalid
instance EllipticCurveArith Curve_P256R1 where
@ -202,6 +202,11 @@ instance EllipticCurveDH Curve_X448 where
ecdh _ s p = SharedSecret $ convert secret
where secret = X448.dh p s
validateP256Point :: P256.Point -> CryptoFailable P256.Point
validateP256Point p
| P256.pointIsValid p = CryptoPassed p
| otherwise = CryptoFailed $ CryptoError_PointCoordinatesInvalid
encodeECPoint :: forall curve bs . (Simple.Curve curve, ByteArray bs) => Simple.Point curve -> bs
encodeECPoint Simple.PointO = error "encodeECPoint: cannot serialize point at infinity"
encodeECPoint (Simple.Point x y) = B.concat [uncompressed,xb,yb]