aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/ec
diff options
context:
space:
mode:
authorJ08nY2023-08-25 17:18:00 +0200
committerJ08nY2023-08-25 17:18:00 +0200
commit76a8a9bbd2684c8a6e554d753c671bdbdb1009b2 (patch)
tree6d073bbeb11feb3d67685d08146226ef12098080 /test/ec
parenta00c495586ceb0375f6a52d4902b4a9df3aec8fb (diff)
downloadpyecsca-76a8a9bbd2684c8a6e554d753c671bdbdb1009b2.tar.gz
pyecsca-76a8a9bbd2684c8a6e554d753c671bdbdb1009b2.tar.zst
pyecsca-76a8a9bbd2684c8a6e554d753c671bdbdb1009b2.zip
Add test-vector from SECG to ECDH tests.
Diffstat (limited to 'test/ec')
-rw-r--r--test/ec/test_key_agreement.py44
1 files changed, 43 insertions, 1 deletions
diff --git a/test/ec/test_key_agreement.py b/test/ec/test_key_agreement.py
index 14bd138..392b21f 100644
--- a/test/ec/test_key_agreement.py
+++ b/test/ec/test_key_agreement.py
@@ -1,4 +1,10 @@
+from copy import copy
+
import pytest
+import json
+from importlib_resources import files
+
+from pyecsca.ec.coordinates import AffineCoordinateModel
from pyecsca.ec.key_agreement import (
ECDH_NONE,
ECDH_SHA1,
@@ -9,6 +15,9 @@ from pyecsca.ec.key_agreement import (
)
from pyecsca.ec.mod import Mod
from pyecsca.ec.mult import LTRMultiplier
+import test.data.ec
+from pyecsca.ec.params import get_params
+from pyecsca.ec.point import Point
@pytest.fixture()
@@ -40,4 +49,37 @@ def test_ka(algo, mult, secp128r1, keypair_a, keypair_b):
result_ba = algo(mult, secp128r1, keypair_b[1], keypair_a[0]).perform()
assert result_ab == result_ba
-# TODO: Add KAT-based tests here.
+
+def test_ka_secg():
+ with files(test.data.ec).joinpath("ecdh_tv.json").open("r") as f:
+ secg_data = json.load(f)
+ secp160r1 = get_params("secg", "secp160r1", "projective")
+ affine_model = AffineCoordinateModel(secp160r1.curve.model)
+ add = secp160r1.curve.coordinate_model.formulas["add-2016-rcb"]
+ dbl = secp160r1.curve.coordinate_model.formulas["dbl-2016-rcb"]
+ mult = LTRMultiplier(add, dbl)
+ privA = Mod(int(secg_data["keyA"]["priv"], 16), secp160r1.order)
+ pubA_affine = Point(affine_model,
+ x=Mod(int(secg_data["keyA"]["pub"]["x"], 16), secp160r1.curve.prime),
+ y=Mod(int(secg_data["keyA"]["pub"]["y"], 16), secp160r1.curve.prime))
+ pubA = pubA_affine.to_model(secp160r1.curve.coordinate_model, secp160r1.curve)
+ privB = Mod(int(secg_data["keyB"]["priv"], 16), secp160r1.order)
+ pubB_affine = Point(affine_model,
+ x=Mod(int(secg_data["keyB"]["pub"]["x"], 16), secp160r1.curve.prime),
+ y=Mod(int(secg_data["keyB"]["pub"]["y"], 16), secp160r1.curve.prime))
+ pubB = pubB_affine.to_model(secp160r1.curve.coordinate_model, secp160r1.curve)
+
+ algoAB = ECDH_SHA1(copy(mult), secp160r1, pubA, privB)
+ resAB = algoAB.perform()
+ algoBA = ECDH_SHA1(copy(mult), secp160r1, pubB, privA)
+ resBA = algoBA.perform()
+
+ assert resAB == resBA
+ assert resAB == bytes.fromhex(secg_data["sha1"])
+
+ resAB_raw = algoAB.perform_raw()
+ x = int(resAB_raw.x)
+ p = secp160r1.curve.prime
+ n = (p.bit_length() + 7) // 8
+ result = x.to_bytes(n, byteorder="big")
+ assert result == bytes.fromhex(secg_data["raw"])