aboutsummaryrefslogtreecommitdiff
path: root/analysis/countermeasures/measure.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'analysis/countermeasures/measure.ipynb')
-rw-r--r--analysis/countermeasures/measure.ipynb1607
1 files changed, 1607 insertions, 0 deletions
diff --git a/analysis/countermeasures/measure.ipynb b/analysis/countermeasures/measure.ipynb
new file mode 100644
index 0000000..2e963f1
--- /dev/null
+++ b/analysis/countermeasures/measure.ipynb
@@ -0,0 +1,1607 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "41f6e89a",
+ "metadata": {},
+ "source": [
+ "# Card measurement for tests\n",
+ "\n",
+ "This notebook contains the code used to obtain measurements from the ECTester applet running on the target smartcards. These measurement results can then be used by the [results.ipynb](results.ipynb) notebook which interprets them."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "1e1e1320",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from pyecsca.sca.target.ectester import KeypairEnum, ParameterEnum, CurveEnum, KeyEnum, KeyClassEnum, KeyBuildEnum, KeyAgreementEnum, SignatureEnum, TransformationEnum\n",
+ "from pyecsca.ec.params import load_params_ectester\n",
+ "from pyecsca.sca.target.ectester import ECTesterTargetPCSC, KeyAgreementEnum\n",
+ "from pyecsca.sca.target.PCSC import PCSCTarget\n",
+ "from smartcard.System import readers\n",
+ "import numpy as np\n",
+ "from time import sleep\n",
+ "from utils import *\n",
+ "import os"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "17e494b2-ca35-4cd7-84b5-d797c4a61bc1",
+ "metadata": {},
+ "source": [
+ "Here, you need to select a reader out of those PCSC sees. "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "668d391e",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "rs = readers()\n",
+ "for reader in rs:\n",
+ " if \"Gemalto\" in reader.name:\n",
+ " break\n",
+ "target = ECTesterTargetPCSC(reader)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "5d4c75be-86e4-4924-82e3-5a75d48ed088",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.connect()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "88c049c9-ba24-41a7-830d-a9e81ecd5e67",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.select_applet()"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "f65d7cb5-b54e-48bc-9a67-153b964f5df1",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "CARD = \"N9\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ac9a16f7-ef19-4b05-86bd-f50b2b070037",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "atr = target.atr.hex()\n",
+ "card_map = {\n",
+ " \"3bd518ff8191fe1fc38073c8211309\": \"A1\",\n",
+ " \"3bb89600c00831fe45ffff1154305023006a\": \"I1\",\n",
+ " \"3bfe1800008031fe45803180664090a5102e1083019000f2\": \"I2\",\n",
+ " \"3bf81800ff8131fe454a434f507632343143\": \"N1\",\n",
+ " \"3bf81300008131fe454a434f5076323431b7\": \"N2N9\",\n",
+ " \"3b9495810146545601c4\": \"N4N10\",\n",
+ " \"3bd518ff8191fe1fc38073c821100a\": \"N6N11\",\n",
+ " \"3b9c9580811f039067464a01005404f272fe00c0\": \"F1\",\n",
+ " \"3b90958011fe6a\": \"F2F3\",\n",
+ " \"3b9f95803fc7a08031e073fa21106300000083f09000bb\": \"S1S2\",\n",
+ " \"3bf91300008131fe454a434f503234325233a2\":\"N8\",\n",
+ " \"3bf99600008131fe4553434537200e00202028\":\"G1\",\n",
+ " \"3b959540ffae01030000\":\"E2\",\n",
+ " \"3bfe1800008031fe4553434536302d43443038312d6e46a9\": \"G2\"\n",
+ "}\n",
+ "print(f\"{card_map[atr]} connected, {CARD} selected\")\n",
+ "assert CARD in card_map[atr]\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "ce6f80b6-d1d3-4bcd-9dfd-720d26e7113f",
+ "metadata": {},
+ "source": [
+ "### Tests\n",
+ "\n",
+ "[Basic ECDH](#Basic-ECDH)\n",
+ "\n",
+ "[Test cofactor ECDH](#Test-cofactor-ECDH)\n",
+ "\n",
+ "[Test3n ECDH](#Test3n-ECDH)\n",
+ "\n",
+ "[Test3n ECDSA](#Test3n-ECDSA)\n",
+ "\n",
+ "[Test3n Keygen](#Test3n-Keygen)\n",
+ "\n",
+ "[Test composite ECDH](#Test-composite-ECDH)\n",
+ "\n",
+ "[Test composite ECDSA](#Test-composite-ECDSA)\n",
+ "\n",
+ "[Test composite Keygen](#Test-composite-Keygen)\n",
+ "\n",
+ "[Test k=10 ECDH](#Test-k=10-ECDH)\n",
+ "\n",
+ "[Test n+epsilon ECDH](#Test-n+epsilon-ECDH)\n",
+ "\n",
+ "[Test n+epsilon ECDSA](#Test-n+epsilon-ECDSA)\n",
+ "\n",
+ "[Test n+epsilon Keygen](#Test-n+epsilon-Keygen)\n",
+ "\n",
+ "[Test small subgroup attack on verify](#Test-small-subgroup-attack-on-verify)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "75ff1c08",
+ "metadata": {},
+ "source": [
+ "### Basic ECDH"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "a567d2a5",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "params = load_params_ectester(\"tests/testk10/secp256r1.csv\", \"affine\")\n",
+ "params_csv = read_curve_params(\"tests/testk10/secp256r1.csv\")\n",
+ "point = get_point_bytes(\"tests/testk10/secgpoint.csv\")\n",
+ "key = 0x57b4a526c3308537f38cfc7ac4fc6b8eba67c5a16b3cfcde6ba82b484c9f77aa"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "8749a079",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " KeyBuildEnum.BUILD_KEYBUILDER | KeyBuildEnum.BUILD_KEYPAIR,\n",
+ " 256,\n",
+ " KeyClassEnum.ALG_EC_FP)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "531459bb",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate_ka(KeyAgreementEnum.ALG_EC_SVDP_DH)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "1ea90961",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.DOMAIN_FP,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.DOMAIN_FP, params))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "950d75ca",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.S,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.S, key))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "f91465cd",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "result = target.ecdh_direct(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " True,\n",
+ " TransformationEnum.NONE,\n",
+ " KeyAgreementEnum.ALG_EC_SVDP_DH,\n",
+ " point)\n",
+ "print(result)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "2eb7fdb7-9444-48ec-bcde-dbf68b14b1ad",
+ "metadata": {},
+ "source": [
+ "### Test cofactor ECDH"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d461d3a3-12d8-4df0-9ca3-a7d5b27cc739",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "params = load_params_ectester(\"tests/testcofactor/weakcurve.csv\", \"affine\")\n",
+ "params_csv = read_curve_params(\"tests/testcofactor/weakcurve.csv\")\n",
+ "point = get_point_bytes(\"tests/testcofactor/weakcurve_point_4n.csv\")\n",
+ "with open(\"tests/testcofactor/key.csv\") as f:\n",
+ " key = int(f.read(),16)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "8fad24f9-0f25-4ccb-9284-1b31d08a42fb",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " KeyBuildEnum.BUILD_KEYBUILDER | KeyBuildEnum.BUILD_KEYPAIR,\n",
+ " 256,\n",
+ " KeyClassEnum.ALG_EC_FP)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "3daf588f-77d5-4e92-8dbc-ae49a79779e4",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate_ka(KeyAgreementEnum.ALG_EC_SVDP_DH_PLAIN)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "288aacff-18d8-417e-80f3-7b4e3c09990b",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.DOMAIN_FP,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.DOMAIN_FP, params))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "cdd0d614-9e2d-4a85-a1d2-045a73647629",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.S,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.S, key))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d22a09ca-cd35-41ff-a7e8-1845034ccc40",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "result = target.ecdh_direct(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " True,\n",
+ " TransformationEnum.NONE,\n",
+ " KeyAgreementEnum.ALG_EC_SVDP_DH_PLAIN,\n",
+ " point) \n",
+ "if not result.success:\n",
+ " print(result)\n",
+ "result_lines = [serialize_ecdh_response(result,params_csv,point,key)]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d496aaf3-11e3-4b21-a988-b1d28651540f",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "save_ecdh(result_lines,f\"results/{CARD}/testcofactor/ecdh_plain.csv\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "a84108a4-73d0-4eb5-a72f-424763f22474",
+ "metadata": {},
+ "source": [
+ "### Test3n ECDH"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "8be52585-25c1-46c7-b6bc-7165198c7cba",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "params = load_params_ectester(\"tests/test3n/curve_prime_gen.csv\", \"affine\")\n",
+ "params_csv = read_curve_params(\"tests/test3n/curve_prime_gen.csv\")\n",
+ "point = get_point_bytes(\"tests/test3n/point_3n.csv\")\n",
+ "with open(\"tests/test3n/key.csv\") as f:\n",
+ " key = int(f.read(),16)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "e6faa774-d813-40cd-a51f-8ae9a680c880",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "samples = 100"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "a398d6bc-c98a-420d-8996-ff5ecd73c53a",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " KeyBuildEnum.BUILD_KEYBUILDER | KeyBuildEnum.BUILD_KEYPAIR,\n",
+ " 256,\n",
+ " KeyClassEnum.ALG_EC_FP)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "edf7190f-eea5-4f3f-9271-7861ac10b2a6",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate_ka(KeyAgreementEnum.ALG_EC_SVDP_DH)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "6bd6e733-2e8c-451e-8b62-f3075e97e877",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.DOMAIN_FP,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.DOMAIN_FP, params))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "c1858f95-1183-42ac-92c6-190785f7e787",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.S,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.S, key))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "16d4f809-fc4e-4b56-be48-0932bee9df5d",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "result_lines = []\n",
+ "\n",
+ "for _ in range(samples):\n",
+ " result = target.ecdh_direct(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " True,\n",
+ " TransformationEnum.NONE,\n",
+ " KeyAgreementEnum.ALG_EC_SVDP_DH,\n",
+ " point) \n",
+ " if not result.success:\n",
+ " print(result)\n",
+ " result_lines.append(serialize_ecdh_response(result,params_csv,point,key))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "df5ec00b-7683-4886-8ca1-7c30d6620e04",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "save_ecdh(result_lines,f\"results/{CARD}/test3n/ecdh.csv\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "3c30db78-bfba-4e7b-bec7-dca29480c3f5",
+ "metadata": {},
+ "source": [
+ "### Test3n ECDSA"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ad17301a-902f-48e9-936e-f553a1a8f006",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "params = load_params_ectester(\"tests/test3n/curve.csv\", \"affine\")\n",
+ "params_csv = read_curve_params(\"tests/test3n/curve.csv\")\n",
+ "with open(\"tests/test3n/key.csv\") as f:\n",
+ " key = int(f.read(),16)\n",
+ "point_bytes = get_point_bytes(\"tests/test3n/point_key.csv\")\n",
+ "point = params.curve.decode_point(point_bytes)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "82e30c00-9409-4ecb-a049-4c7a4e5e358a",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "samples = 100\n",
+ "fixed_key = True\n",
+ "data = bytes.fromhex(\"e8c86c9596e46403c211206617b8bcdd160a673b0b1304869f5559e3afe99d79\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "975fb0b2-613b-430f-81bf-261e54be2afd",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " KeyBuildEnum.BUILD_KEYBUILDER | KeyBuildEnum.BUILD_KEYPAIR,\n",
+ " 256,\n",
+ " KeyClassEnum.ALG_EC_FP)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "1c4451f0-c628-4f2f-b3e6-4f3573c78e25",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate_sig(SignatureEnum.ALG_ECDSA_SHA)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "2a64c51d-6597-4eaa-8266-c6dca1c1dd5b",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.DOMAIN_FP,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.DOMAIN_FP, params))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "068dba48-c8d8-442e-9a1e-7bc203d17e1b",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "if fixed_key:\n",
+ " keypair_dict = ECTesterTargetPCSC.encode_parameters(ParameterEnum.S, key)\n",
+ " keypair_dict.update(ECTesterTargetPCSC.encode_parameters(ParameterEnum.W, point))\n",
+ " print(target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.KEYPAIR,\n",
+ " keypair_dict))\n",
+ "else:\n",
+ " print(target.generate(KeypairEnum.KEYPAIR_LOCAL))\n",
+ " export = target.export(KeypairEnum.KEYPAIR_LOCAL, KeyEnum.BOTH,ParameterEnum.KEYPAIR)\n",
+ " print(export)\n",
+ " key = int(export.params[1].hex(),16)\n",
+ " point = params.curve.decode_point(export.params[0])\n",
+ " point_bytes = export.params[0]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "1e17dcbf-3578-4893-a86d-668157af69e0",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "result_lines = []\n",
+ "for _ in range(samples):\n",
+ " sign_response = target.ecdsa_sign(KeypairEnum.KEYPAIR_LOCAL,True,SignatureEnum.ALG_ECDSA_SHA,data)\n",
+ " if not sign_response.success:\n",
+ " print(f\"sign: {sign_response.sws}\",end=\", \")\n",
+ " signature = sign_response.signature\n",
+ " verify_response = target.ecdsa_verify(KeypairEnum.KEYPAIR_LOCAL,SignatureEnum.ALG_ECDSA_SHA,signature,data)\n",
+ " if not verify_response.success:\n",
+ " print(f\"verify: {verify_response.sws}\",end=\", \")\n",
+ " result_lines.append(serialize_ecdsa_response(sign_response,data,params,key,params_csv,point_bytes, verify_response.success))\n",
+ " "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "6c677375-2ff5-47bd-a3bb-69ad7014c016",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "if fixed_key:\n",
+ " save_ecdsa(result_lines,f\"results/{CARD}/test3n/ecdsa_fixed.csv\")\n",
+ "else:\n",
+ " save_ecdsa(result_lines,f\"results/{CARD}/test3n/ecdsa.csv\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "0b73b805-5d36-4908-8ba9-e5c3023831c0",
+ "metadata": {},
+ "source": [
+ "### Test3n Keygen"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "4595a86a-7bb8-47cf-b4c9-93babc33a0b2",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "params = load_params_ectester(\"tests/test3n/curve.csv\", \"affine\")\n",
+ "params_csv = read_curve_params(\"tests/test3n/curve.csv\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "8977904d-6e11-43ae-a459-413902b87a2d",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "samples = 100"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "7880e434-1e72-418a-8a90-c6da659c777d",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " KeyBuildEnum.BUILD_KEYBUILDER | KeyBuildEnum.BUILD_KEYPAIR,\n",
+ " 256,\n",
+ " KeyClassEnum.ALG_EC_FP)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "2a081121-1a75-4f19-a495-be9cbc76c136",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.DOMAIN_FP,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.DOMAIN_FP, params))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "013a8a15-b73d-4dea-88a7-67e2c0c73ae8",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "result_lines = []\n",
+ "for _ in range(samples):\n",
+ " generate_response = target.generate(KeypairEnum.KEYPAIR_LOCAL)\n",
+ " if not generate_response.success:\n",
+ " print(f\"generate: {generate_response.sws}\",end=\", \")\n",
+ " export = target.export(KeypairEnum.KEYPAIR_LOCAL, KeyEnum.BOTH,ParameterEnum.KEYPAIR)\n",
+ " if not export.success:\n",
+ " print(f\"export: {export.sws}\",end=\", \")\n",
+ " continue\n",
+ " key = int(export.params[1].hex(),16)\n",
+ " point = params.curve.decode_point(export.params[0])\n",
+ " point_bytes = export.params[0]\n",
+ " result_lines.append(serialize_keygen_response(generate_response,key,params_csv,point_bytes))\n",
+ " "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "2fcbb056-7bc9-40c3-872e-23dec4c96305",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "save_keygen(result_lines,f\"results/{CARD}/test3n/keygen.csv\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "c147a398-a66b-464f-9a4d-0b4978c46307",
+ "metadata": {},
+ "source": [
+ "### Test composite ECDH"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "33e2dd59-6d6a-4bbc-b5d3-8f8a76ff7229",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "params = load_params_ectester(\"tests/testinverse/cofactor256p11_full.csv\", \"affine\")\n",
+ "params_csv = read_curve_params(\"tests/testinverse/cofactor256p11_full.csv\")\n",
+ "point_bytes = get_point_bytes(\"tests/testinverse/point_11n.csv\")\n",
+ "with open(\"tests/testinverse/key.csv\") as f:\n",
+ " key = int(f.read(),16)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "4e5c98f0-8c7a-4330-ad9c-7462ae4c87ab",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "samples = 100"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "e2af6efc",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " KeyBuildEnum.BUILD_KEYBUILDER | KeyBuildEnum.BUILD_KEYPAIR,\n",
+ " 256,\n",
+ " KeyClassEnum.ALG_EC_FP)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "a9aba5f9-045f-404e-b144-cbac0f73888a",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate_ka(KeyAgreementEnum.ALG_EC_SVDP_DH)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "a9e510a8-019e-4817-a3b4-58cdb51bbbdb",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.DOMAIN_FP,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.DOMAIN_FP, params))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "4cef1b6b-1c70-4a64-aa54-4f7f1c420cd7",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.S,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.S, key))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d4bf4415-6a06-47aa-970a-1d33426435c3",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "result_lines = []\n",
+ "\n",
+ "for _ in range(samples):\n",
+ " result = target.ecdh_direct(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " True,\n",
+ " TransformationEnum.NONE,\n",
+ " KeyAgreementEnum.ALG_EC_SVDP_DH,\n",
+ " point_bytes) \n",
+ " if not result.success:\n",
+ " print(result)\n",
+ " result_lines.append(serialize_ecdh_response(result,params_csv,point_bytes,key))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "fe139916-e09f-4714-b74d-4a4e6f0ee268",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "save_ecdh(result_lines,f\"results/{CARD}/testinverse/ecdh.csv\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "519acbea-ded4-471c-8f4a-1664fc276455",
+ "metadata": {},
+ "source": [
+ "### Test composite ECDSA"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d965ad90-8692-4152-a3bf-9641e058a9f2",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "params = load_params_ectester(\"tests/testinverse/cofactor256p11_full.csv\", \"affine\")\n",
+ "params_csv = read_curve_params(\"tests/testinverse/cofactor256p11_full.csv\")\n",
+ "point_bytes = get_point_bytes(\"tests/testinverse/point_11n.csv\")\n",
+ "point = params.curve.decode_point(point_bytes)\n",
+ "\n",
+ "with open(\"tests/testinverse/key.csv\") as f:\n",
+ " key = int(f.read(),16)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "6859176c-c60c-4aec-a18d-90e1c5e59f78",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "samples = 100\n",
+ "fixed_key = False\n",
+ "data = bytes.fromhex(\"e8c86c9596e46403c211206617b8bcdd160a673b0b1304869f5559e3afe99d79\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "9f4b63d6",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " KeyBuildEnum.BUILD_KEYBUILDER | KeyBuildEnum.BUILD_KEYPAIR,\n",
+ " 256,\n",
+ " KeyClassEnum.ALG_EC_FP)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "7c38ca2a-417e-450d-8093-a9f2c757f26b",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate_sig(SignatureEnum.ALG_ECDSA_SHA)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "38f4499a-97ad-49ba-aec9-d99aa264867c",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.DOMAIN_FP,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.DOMAIN_FP, params))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "7b1a48cc-6aa3-480e-b9a6-f82b0417275a",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "if fixed_key:\n",
+ " keypair_dict = ECTesterTargetPCSC.encode_parameters(ParameterEnum.S, key)\n",
+ " keypair_dict.update(ECTesterTargetPCSC.encode_parameters(ParameterEnum.W, point))\n",
+ " print(target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.KEYPAIR,\n",
+ " keypair_dict))\n",
+ "else:\n",
+ " print(target.generate(KeypairEnum.KEYPAIR_LOCAL))\n",
+ " export = target.export(KeypairEnum.KEYPAIR_LOCAL, KeyEnum.BOTH,ParameterEnum.KEYPAIR)\n",
+ " print(export)\n",
+ " key = int(export.params[1].hex(),16)\n",
+ " point = params.curve.decode_point(export.params[0])\n",
+ " point_bytes = export.params[0]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "3b04e99a-43f4-4cf1-9f2c-d15c541cbc1a",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "result_lines = []\n",
+ "for _ in range(samples):\n",
+ " sign_response = target.ecdsa_sign(KeypairEnum.KEYPAIR_LOCAL,True,SignatureEnum.ALG_ECDSA_SHA,data)\n",
+ " if not sign_response.success:\n",
+ " print(f\"sign: {sign_response.sws}\",end=\", \")\n",
+ " signature = sign_response.signature\n",
+ " verify_response = target.ecdsa_verify(KeypairEnum.KEYPAIR_LOCAL,SignatureEnum.ALG_ECDSA_SHA,signature,data)\n",
+ " if not verify_response.success:\n",
+ " print(f\"verify: {verify_response.sws}\",end=\", \")\n",
+ " result_lines.append(serialize_ecdsa_response(sign_response,data,params,key,params_csv,point_bytes, verify_response.success))\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "50389671-934f-45ad-9511-0b46af7cecd5",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "if fixed_key:\n",
+ " save_ecdsa(result_lines,f\"results/{CARD}/testinverse/ecdsa_fixed.csv\")\n",
+ "else:\n",
+ " save_ecdsa(result_lines,f\"results/{CARD}/testinverse/ecdsa.csv\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "853c5302-2a4f-4798-8d06-e030dc686c38",
+ "metadata": {},
+ "source": [
+ "### Test composite Keygen"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d360b9c5-2076-481c-baa9-0524098e5f27",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "params = load_params_ectester(\"tests/testinverse/cofactor256p11_full.csv\", \"affine\")\n",
+ "params_csv = read_curve_params(\"tests/testinverse/cofactor256p11_full.csv\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "0b7dba4e-39a1-4559-8065-8f40074c1792",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "samples = 10"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "8bc87547-1186-44f8-b575-d61fd247d92c",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " KeyBuildEnum.BUILD_KEYBUILDER | KeyBuildEnum.BUILD_KEYPAIR,\n",
+ " 256,\n",
+ " KeyClassEnum.ALG_EC_FP)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "087ec5a5-c336-49f5-858a-6982dba8cba8",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.DOMAIN_FP,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.DOMAIN_FP, params))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ca66f4a8-0592-41fd-b585-af564782bcc2",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "result_lines = []\n",
+ "for _ in range(samples):\n",
+ " generate_response = target.generate(KeypairEnum.KEYPAIR_LOCAL)\n",
+ " if not generate_response.success:\n",
+ " print(f\"generate: {generate_response.sws}\",end=\", \")\n",
+ " export = target.export(KeypairEnum.KEYPAIR_LOCAL, KeyEnum.BOTH,ParameterEnum.KEYPAIR)\n",
+ " if not export.success:\n",
+ " print(f\"export: {export.sws}\",end=\", \")\n",
+ " continue\n",
+ " key = int(export.params[1].hex(),16)\n",
+ " point = params.curve.decode_point(export.params[0])\n",
+ " point_bytes = export.params[0]\n",
+ " result_lines.append(serialize_keygen_response(generate_response,key,params_csv,point_bytes))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "136b6482-1fab-4380-8c8b-89d453845d58",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "save_keygen(result_lines,f\"results/{CARD}/testinverse/keygen.csv\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "55b6a278-ab84-4c97-a9f1-b379699cbff5",
+ "metadata": {},
+ "source": [
+ "### Test k=10 ECDH"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "aaab3211-8b0b-41fa-bb68-a65234f32880",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "params = load_params_ectester(\"tests/testk10/secp256r1.csv\", \"affine\")\n",
+ "params_csv = read_curve_params(\"tests/testk10/secp256r1.csv\")\n",
+ "point_bytes = get_point_bytes(\"tests/testk10/secgpoint.csv\")\n",
+ "with open(\"tests/testk10/key_10.csv\") as f:\n",
+ " key = int(f.read(),16)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "fc60fce3-944a-414a-8b0a-fafbe1db2a4b",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "samples = 100"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "42a8dae9",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " KeyBuildEnum.BUILD_KEYBUILDER | KeyBuildEnum.BUILD_KEYPAIR,\n",
+ " 256,\n",
+ " KeyClassEnum.ALG_EC_FP)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "2a646481-71c0-4c65-b4ba-62957ae72a3f",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate_ka(KeyAgreementEnum.ALG_EC_SVDP_DH)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "1493b7cc-e5e6-46a6-a2e5-27247e670cfb",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.DOMAIN_FP,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.DOMAIN_FP, params))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "b2296297-23d9-4af9-8d8d-5dbd2db1004a",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.S,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.S, key))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "1ed11d52-d7cd-471e-9ac3-02b9248dfb9a",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "result_lines = []\n",
+ "\n",
+ "for _ in range(samples):\n",
+ " result = target.ecdh_direct(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " True,\n",
+ " TransformationEnum.NONE,\n",
+ " KeyAgreementEnum.ALG_EC_SVDP_DH,\n",
+ " point_bytes)\n",
+ " if not result.success:\n",
+ " print(result)\n",
+ " result_lines.append(serialize_ecdh_response(result,params_csv,point_bytes,key))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d88d32e1-c158-4eb9-855f-a18411d186b0",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "save_ecdh(result_lines,f\"results/{CARD}/testk10/ecdh.csv\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "71daf414",
+ "metadata": {},
+ "source": [
+ "### Test n+epsilon ECDH"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "000e3355",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "params = load_params_ectester(\"tests/testdn/weakcurve_32_n_good_gen.csv\", \"affine\")\n",
+ "params_csv = read_curve_params(\"tests/testdn/weakcurve_32_n_good_gen.csv\")\n",
+ "point = get_point_bytes(\"tests/testdn/weakcurve_32_n_1_point.csv\")\n",
+ "with open(\"tests/testdn/key.csv\") as f:\n",
+ " key = int(f.read(),16)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "2cda23aa",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "samples = 100"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "23f039a0",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " KeyBuildEnum.BUILD_KEYBUILDER | KeyBuildEnum.BUILD_KEYPAIR,\n",
+ " 256,\n",
+ " KeyClassEnum.ALG_EC_FP)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "47a4be1d",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate_ka(KeyAgreementEnum.ALG_EC_SVDP_DH_PLAIN)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "f1ebadac",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.DOMAIN_FP,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.DOMAIN_FP, params))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "fd3f56ea",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.S,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.S, key))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "7975ecd8",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "result_lines = []\n",
+ "\n",
+ "for i in range(samples):\n",
+ " result = target.ecdh_direct(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " True,\n",
+ " TransformationEnum.NONE,\n",
+ " KeyAgreementEnum.ALG_EC_SVDP_DH_PLAIN,\n",
+ " point) \n",
+ " if not result.success:\n",
+ " print(result)\n",
+ " result_lines.append(serialize_ecdh_response(result,params_csv,point,key))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "c5327dab",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ " save_ecdh(result_lines,f\"results/{CARD}/testdn/ecdh.csv\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "f26e2d2b-b6b6-4ae1-9474-46bd3c73b1b4",
+ "metadata": {},
+ "source": [
+ "### Test n+epsilon ECDSA"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ad0f0e52-9a64-41fe-8464-edbace9e3bb0",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "params = load_params_ectester(\"tests/testdn/weakcurve_32_n_1.csv\", \"affine\")\n",
+ "params_csv = read_curve_params(\"tests/testdn/weakcurve_32_n_1.csv\")\n",
+ "point_bytes = get_point_bytes(\"tests/testdn/key_point.csv\")\n",
+ "point = params.curve.decode_point(point_bytes)\n",
+ "bits = 256\n",
+ "with open(\"tests/testdn/key.csv\") as f:\n",
+ " key = int(f.read(),16)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "a27c974e-5adb-482f-b3f3-c4a1bd2a00e4",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "samples = 10\n",
+ "data = bytes.fromhex(\"e8c86c9596e46403c211206617b8bcdd160a673b0b1304869f5559e3afe99d79\")\n",
+ "fixed_key = True"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "3a29e9df-87b7-4e4d-a8d0-796a0b09e678",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " KeyBuildEnum.BUILD_KEYBUILDER | KeyBuildEnum.BUILD_KEYPAIR,\n",
+ " bits,\n",
+ " KeyClassEnum.ALG_EC_FP)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "351c3451-f327-4ea6-9a1f-1057af63cb37",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate_sig(SignatureEnum.ALG_ECDSA_SHA)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "5699c6f6-dd87-46d7-a2b8-cdd4a4c67624",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.DOMAIN_FP,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.DOMAIN_FP, params))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d2e691a6-1ad1-4df8-bf77-6ad9852d3c78",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "if fixed_key:\n",
+ " keypair_dict = ECTesterTargetPCSC.encode_parameters(ParameterEnum.S, key)\n",
+ " keypair_dict.update(ECTesterTargetPCSC.encode_parameters(ParameterEnum.W, point))\n",
+ " print(target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.KEYPAIR,\n",
+ " keypair_dict))\n",
+ "else:\n",
+ " print(target.generate(KeypairEnum.KEYPAIR_LOCAL))\n",
+ " export = target.export(KeypairEnum.KEYPAIR_LOCAL, KeyEnum.BOTH,ParameterEnum.KEYPAIR)\n",
+ " print(export)\n",
+ " key = int(export.params[1].hex(),16)\n",
+ " point = params.curve.decode_point(export.params[0])\n",
+ " point_bytes = export.params[0]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "610f636a",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.ecdsa_sign(KeypairEnum.KEYPAIR_LOCAL,True,SignatureEnum.ALG_ECDSA_SHA,data)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "e4b3b492-4aa4-4a62-bcde-8c49e5fdc0da",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "result_lines = []\n",
+ "for _ in range(samples):\n",
+ " sign_response = target.ecdsa_sign(KeypairEnum.KEYPAIR_LOCAL,True,SignatureEnum.ALG_ECDSA_SHA,data)\n",
+ " if not sign_response.success:\n",
+ " print(f\"sign: {sign_response.sws}\",end=\", \")\n",
+ " result_lines.append(serialize_ecdsa_response(sign_response,data,params,key,params_csv,point_bytes, None))\n",
+ " "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "bd03c4b5-99ac-43ab-ac20-12f7639a5632",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ " save_ecdsa(result_lines,f\"results/{CARD}/tesdn/ecdsa.csv\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "46188f91",
+ "metadata": {},
+ "source": [
+ "### Test n+epsilon Keygen"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "981c3bf7",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "params = load_params_ectester(\"tests/testdn/weakcurve_32_n_1.csv\", \"affine\")\n",
+ "params_csv = read_curve_params(\"tests/testdn/weakcurve_32_n_1.csv\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "8193d2e8",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "samples = 10"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "53f58db7",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " KeyBuildEnum.BUILD_KEYBUILDER | KeyBuildEnum.BUILD_KEYPAIR,\n",
+ " 256,\n",
+ " KeyClassEnum.ALG_EC_FP)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "7a6bd8fc",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.DOMAIN_FP,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.DOMAIN_FP, params))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "1baeeffe",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "result_lines = []\n",
+ "for _ in range(samples):\n",
+ " generate_response = target.generate(KeypairEnum.KEYPAIR_LOCAL)\n",
+ " if not generate_response.success:\n",
+ " print(f\"generate: {generate_response.sws}\",end=\", \")\n",
+ " export = target.export(KeypairEnum.KEYPAIR_LOCAL, KeyEnum.BOTH,ParameterEnum.KEYPAIR)\n",
+ " if not export.success:\n",
+ " print(f\"export: {export.sws}\",end=\", \")\n",
+ " continue\n",
+ " key = int(export.params[1].hex(),16)\n",
+ " point = params.curve.decode_point(export.params[0])\n",
+ " point_bytes = export.params[0]\n",
+ " result_lines.append(serialize_keygen_response(generate_response,key,params_csv,point_bytes))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "1689f405",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "save_keygen(result_lines,f\"results/{CARD}/testdn/keygen.csv\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "e5dc1b1b-2015-4e7d-844d-a72be5a8f97d",
+ "metadata": {},
+ "source": [
+ "### Test small subgroup attack on verify"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "cae5894c-6b10-4311-be58-796a7f5b8496",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from pyecsca.ec.mod import Mod, RawMod\n",
+ "from pyasn1.codec.der.decoder import decode\n",
+ "from pyasn1.type.univ import Sequence\n",
+ "\n",
+ "def parse_ecdsa_signature(signature_der):\n",
+ " decoded_signature, _ = decode(signature_der, asn1Spec=Sequence())\n",
+ " r = int(decoded_signature[0])\n",
+ " s = int(decoded_signature[1])\n",
+ " return r, s\n",
+ "\n",
+ "def is_r_even(signature_der, n):\n",
+ " r,s = parse_ecdsa_signature(signature_der)\n",
+ " scalar = int(r*RawMod(s,n).inverse())\n",
+ " return scalar%2==0 and r%2==0"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "b89f9209-e956-4bf6-9647-eec318aa4505",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "params = load_params_ectester(\"tests/verify2/cofactor256p2_h.csv\", \"affine\")\n",
+ "params_csv = read_curve_params(\"tests/verify2/cofactor256p2_h.csv\")\n",
+ "point_bytes = get_point_bytes(\"tests/verify2/key_point.csv\")\n",
+ "point = params.curve.decode_point(point_bytes)\n",
+ "point_wrong_bytes = get_point_bytes(\"tests/verify2/key_point_wrong.csv\")\n",
+ "point_wrong = params.curve.decode_point(point_wrong_bytes)\n",
+ "point_realwrong_bytes = get_point_bytes(\"tests/verify2/key_point_realwrong.csv\")\n",
+ "point_realwrong = params.curve.decode_point(point_realwrong_bytes)\n",
+ "with open(\"tests/testdn/key.csv\") as f:\n",
+ " key = int(f.read(),16)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "2ba741cf-3dd8-4ce9-a097-887862ee6afd",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "data = bytes.fromhex(\"e8c86c9596e46403c211206617b8bcdd160a673b0b1304869f5559e3afe99d79\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "9a723e27-e174-4ade-a104-571185170b8c",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " KeyBuildEnum.BUILD_KEYBUILDER | KeyBuildEnum.BUILD_KEYPAIR,\n",
+ " 256,\n",
+ " KeyClassEnum.ALG_EC_FP)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "e6f06396-5fab-43c6-9c27-14d210479546",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.allocate_sig(SignatureEnum.ALG_ECDSA_SHA)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "6998533e-794b-44fb-b186-f30abb517d60",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.DOMAIN_FP,\n",
+ " ECTesterTargetPCSC.encode_parameters(ParameterEnum.DOMAIN_FP, params))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "6c947e4c-44c1-462f-9702-1d3f1253ea8b",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "keypair_dict = ECTesterTargetPCSC.encode_parameters(ParameterEnum.S, key)\n",
+ "keypair_dict.update(ECTesterTargetPCSC.encode_parameters(ParameterEnum.W, point))\n",
+ "print(target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.KEYPAIR,\n",
+ " keypair_dict))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "e93c5153-4630-45c5-8b2c-10ce4b7b6bda",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "sign_response = target.ecdsa_sign(KeypairEnum.KEYPAIR_LOCAL,True,SignatureEnum.ALG_ECDSA_SHA,data)\n",
+ "print(is_r_even(sign_response.signature,params.full_order//2))\n",
+ "sign_response"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "09c1c1a4-d96d-41f4-a58a-f7551bd671e1",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.ecdsa_verify(KeypairEnum.KEYPAIR_LOCAL,SignatureEnum.ALG_ECDSA_SHA,sign_response.signature,data)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "ea58b47f-1bd7-4717-852e-526ff33090ea",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "keypair_dict = ECTesterTargetPCSC.encode_parameters(ParameterEnum.S, key)\n",
+ "keypair_dict.update(ECTesterTargetPCSC.encode_parameters(ParameterEnum.W, point_wrong))\n",
+ "print(target.set(KeypairEnum.KEYPAIR_LOCAL,\n",
+ " CurveEnum.external,\n",
+ " ParameterEnum.KEYPAIR,\n",
+ " keypair_dict))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "6ebd8564-02dc-4cba-9d16-953ca7f858cf",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "target.ecdsa_verify(KeypairEnum.KEYPAIR_LOCAL,SignatureEnum.ALG_ECDSA_SHA,sign_response.signature,data)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "afaab408-1800-4188-a290-07f12b84fe04",
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.13.5"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}