aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorJ08nY2023-08-25 17:18:00 +0200
committerJ08nY2023-08-25 17:18:00 +0200
commit76a8a9bbd2684c8a6e554d753c671bdbdb1009b2 (patch)
tree6d073bbeb11feb3d67685d08146226ef12098080 /test
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')
-rw-r--r--test/data/ec/ecdh_tv.json18
-rw-r--r--test/ec/test_key_agreement.py44
2 files changed, 61 insertions, 1 deletions
diff --git a/test/data/ec/ecdh_tv.json b/test/data/ec/ecdh_tv.json
new file mode 100644
index 0000000..bf12d91
--- /dev/null
+++ b/test/data/ec/ecdh_tv.json
@@ -0,0 +1,18 @@
+{
+ "raw": "ca7c0f8c3ffa87a96e1b74ac8e6af594347bb40a",
+ "sha1": "d248313e865a1ae677782b54b24d8abaf11a53c2",
+ "keyA": {
+ "priv": "0xaa374ffc3ce144e6b073307972cb6d57b2a4e982",
+ "pub": {
+ "x": "0x51b4496fecc406ed0e75a24a3c03206251419dc0",
+ "y": "0xc28dcb4b73a514b468d793894f381ccc1756aa6c"
+ }
+ },
+ "keyB": {
+ "priv": "0x45fb58a92a17ad4b15101c66e74f277e2b460866",
+ "pub": {
+ "x": "0x49b41e0e9c0369c2328739d90f63d56707c6e5bc",
+ "y": "0x26e008b567015ed96d232a03111c3edc0e9c8f83"
+ }
+ }
+}
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"])