Validate output point when calling P256.pointFromBinary
Function unsafePointFromBinary is added when validation is not needed.
This commit is contained in:
parent
099f3405cb
commit
8e274f8e60
@ -118,7 +118,7 @@ instance EllipticCurve Curve_P256R1 where
|
|||||||
Nothing -> CryptoFailed $ CryptoError_PointSizeInvalid
|
Nothing -> CryptoFailed $ CryptoError_PointSizeInvalid
|
||||||
Just (m,xy)
|
Just (m,xy)
|
||||||
-- uncompressed
|
-- uncompressed
|
||||||
| m == 4 -> P256.pointFromBinary xy >>= validateP256Point
|
| m == 4 -> P256.pointFromBinary xy
|
||||||
| otherwise -> CryptoFailed $ CryptoError_PointFormatInvalid
|
| otherwise -> CryptoFailed $ CryptoError_PointFormatInvalid
|
||||||
|
|
||||||
instance EllipticCurveArith Curve_P256R1 where
|
instance EllipticCurveArith Curve_P256R1 where
|
||||||
@ -210,11 +210,6 @@ instance EllipticCurveDH Curve_X448 where
|
|||||||
ecdh _ s p = SharedSecret $ convert secret
|
ecdh _ s p = SharedSecret $ convert secret
|
||||||
where secret = X448.dh p s
|
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 :: forall curve bs . (Simple.Curve curve, ByteArray bs) => Simple.Point curve -> bs
|
||||||
encodeECPoint Simple.PointO = error "encodeECPoint: cannot serialize point at infinity"
|
encodeECPoint Simple.PointO = error "encodeECPoint: cannot serialize point at infinity"
|
||||||
encodeECPoint (Simple.Point x y) = B.concat [uncompressed,xb,yb]
|
encodeECPoint (Simple.Point x y) = B.concat [uncompressed,xb,yb]
|
||||||
|
|||||||
@ -26,6 +26,7 @@ module Crypto.PubKey.ECC.P256
|
|||||||
, pointFromIntegers
|
, pointFromIntegers
|
||||||
, pointToBinary
|
, pointToBinary
|
||||||
, pointFromBinary
|
, pointFromBinary
|
||||||
|
, unsafePointFromBinary
|
||||||
-- * scalar arithmetic
|
-- * scalar arithmetic
|
||||||
, scalarGenerate
|
, scalarGenerate
|
||||||
, scalarZero
|
, scalarZero
|
||||||
@ -172,9 +173,18 @@ pointToBinary p = B.unsafeCreate pointSize $ \dst -> withPoint p $ \px py -> do
|
|||||||
ccryptonite_p256_to_bin (castPtr px) dst
|
ccryptonite_p256_to_bin (castPtr px) dst
|
||||||
ccryptonite_p256_to_bin (castPtr py) (dst `plusPtr` 32)
|
ccryptonite_p256_to_bin (castPtr py) (dst `plusPtr` 32)
|
||||||
|
|
||||||
-- | Convert from binary to a point
|
-- | Convert from binary to a valid point
|
||||||
pointFromBinary :: ByteArrayAccess ba => ba -> CryptoFailable Point
|
pointFromBinary :: ByteArrayAccess ba => ba -> CryptoFailable Point
|
||||||
pointFromBinary ba
|
pointFromBinary ba = unsafePointFromBinary ba >>= validatePoint
|
||||||
|
where
|
||||||
|
validatePoint :: Point -> CryptoFailable Point
|
||||||
|
validatePoint p
|
||||||
|
| pointIsValid p = CryptoPassed p
|
||||||
|
| otherwise = CryptoFailed $ CryptoError_PointCoordinatesInvalid
|
||||||
|
|
||||||
|
-- | Convert from binary to a point, possibly invalid
|
||||||
|
unsafePointFromBinary :: ByteArrayAccess ba => ba -> CryptoFailable Point
|
||||||
|
unsafePointFromBinary ba
|
||||||
| B.length ba /= pointSize = CryptoFailed $ CryptoError_PublicKeySizeInvalid
|
| B.length ba /= pointSize = CryptoFailed $ CryptoError_PublicKeySizeInvalid
|
||||||
| otherwise =
|
| otherwise =
|
||||||
CryptoPassed $ withNewPoint $ \px py -> B.withByteArray ba $ \src -> do
|
CryptoPassed $ withNewPoint $ \px py -> B.withByteArray ba $ \src -> do
|
||||||
|
|||||||
@ -97,7 +97,7 @@ tests = testGroup "P256"
|
|||||||
[ testProperty "marshalling" $ \rx ry ->
|
[ testProperty "marshalling" $ \rx ry ->
|
||||||
let p = P256.pointFromIntegers (unP256 rx, unP256 ry)
|
let p = P256.pointFromIntegers (unP256 rx, unP256 ry)
|
||||||
b = P256.pointToBinary p :: Bytes
|
b = P256.pointToBinary p :: Bytes
|
||||||
p' = P256.pointFromBinary b
|
p' = P256.unsafePointFromBinary b
|
||||||
in propertyHold [ eqTest "point" (CryptoPassed p) p' ]
|
in propertyHold [ eqTest "point" (CryptoPassed p) p' ]
|
||||||
, testProperty "marshalling-integer" $ \rx ry ->
|
, testProperty "marshalling-integer" $ \rx ry ->
|
||||||
let p = P256.pointFromIntegers (unP256 rx, unP256 ry)
|
let p = P256.pointFromIntegers (unP256 rx, unP256 ry)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user