diff options
| author | J08nY | 2023-02-17 18:40:41 +0100 |
|---|---|---|
| committer | J08nY | 2023-02-17 18:40:41 +0100 |
| commit | 06b53bea04b38564b1205e48d802601aa0d255a2 (patch) | |
| tree | 585d24dd078c20290ab7c381b234a0ebf1d2b62e | |
| parent | 6f99cb164eba8c9da6aa2e7e4caf448e94cc8896 (diff) | |
| download | pyecsca-notebook-06b53bea04b38564b1205e48d802601aa0d255a2.tar.gz pyecsca-notebook-06b53bea04b38564b1205e48d802601aa0d255a2.tar.zst pyecsca-notebook-06b53bea04b38564b1205e48d802601aa0d255a2.zip | |
Add notebook demonstrating smartcards.
| -rw-r--r-- | smartcards.ipynb | 287 |
1 files changed, 287 insertions, 0 deletions
diff --git a/smartcards.ipynb b/smartcards.ipynb new file mode 100644 index 0000000..2fcd993 --- /dev/null +++ b/smartcards.ipynb @@ -0,0 +1,287 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "ade33c98", + "metadata": {}, + "source": [ + "# Smartcards" + ] + }, + { + "cell_type": "markdown", + "id": "2d945579", + "metadata": {}, + "source": [ + "This notebook showcases how to use **pyecsca** to communicate with a smartcard target, either via a PCSC-compatible reader or a [LEIA](https://h2lab.org/devices/leia/boards_leia/) board. The target in this notebook is the [ECTester](https://github.com/crocs-muni/ECTester) applet running on a JavaCard." + ] + }, + { + "cell_type": "markdown", + "id": "b266ea5a", + "metadata": {}, + "source": [ + "But first, a showcase of the raw LEIA and PCSC APIs" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4602cb9a", + "metadata": {}, + "outputs": [], + "source": [ + "from pyecsca.sca.target.leia import LEIATarget\n", + "from pyecsca.sca.target.PCSC import PCSCTarget\n", + "from smartleia import LEIA\n", + "from smartcard.System import readers" + ] + }, + { + "cell_type": "markdown", + "id": "b4a1cb25", + "metadata": {}, + "source": [ + "## LEIA\n", + "For the following lines to work you need a LEIA board connected and a card inserted inside of it." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1edeed22", + "metadata": {}, + "outputs": [], + "source": [ + "sl = LEIA()\n", + "leia = LEIATarget(sl)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eb60372e", + "metadata": {}, + "outputs": [], + "source": [ + "leia.connect()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2b07c714", + "metadata": {}, + "outputs": [], + "source": [ + "leia.atr.hex()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5962f0ee", + "metadata": {}, + "outputs": [], + "source": [ + "leia.select(bytes.fromhex(\"73696D706C656170706C6574\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dd384310", + "metadata": {}, + "outputs": [], + "source": [ + "leia.disconnect()" + ] + }, + { + "cell_type": "markdown", + "id": "8df2666b", + "metadata": {}, + "source": [ + "## PCSC\n", + "For the following lines to work you need a PCSC-compatible reader connected and a card inserted inside of it.\n", + "On Linux you also likely need the [PCSClite](https://pcsclite.apdu.fr/) service running. This code picks the first reader it finds, but it can be adjusted to pick the correct one in a multi-reader setup." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f7ca966c", + "metadata": {}, + "outputs": [], + "source": [ + "rs = readers()\n", + "reader = rs[0]\n", + "pcsc = PCSCTarget(reader)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ac6177b2", + "metadata": {}, + "outputs": [], + "source": [ + "pcsc.connect()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2170c4df", + "metadata": {}, + "outputs": [], + "source": [ + "pcsc.atr.hex()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f4e869a2", + "metadata": {}, + "outputs": [], + "source": [ + "pcsc.select(bytes.fromhex(\"73696D706C656170706C6574\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "33cbb320", + "metadata": {}, + "outputs": [], + "source": [ + "pcsc.disconnect()" + ] + }, + { + "cell_type": "markdown", + "id": "91d2746b", + "metadata": {}, + "source": [ + "## ECTester\n", + "The following lines assume that the [ECTester](https://github.com/crocs-muni/ECTester) applet is installed on a card connected via a PCSC-compatible reader." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7e37dd61", + "metadata": {}, + "outputs": [], + "source": [ + "from pyecsca.sca.target.ectester import ECTesterTargetPCSC, KeyAgreementEnum" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f6c64af4", + "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": "69d047e2", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "target.connect()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cc92ebb2", + "metadata": {}, + "outputs": [], + "source": [ + "target.select_applet()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c10b744d", + "metadata": {}, + "outputs": [], + "source": [ + "target.atr.hex()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f8d55215", + "metadata": {}, + "outputs": [], + "source": [ + "target.allocate_ka(KeyAgreementEnum.ALG_EC_SVDP_DH_PLAIN)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ba2b2ba9", + "metadata": {}, + "outputs": [], + "source": [ + "target.info()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "74df43e9", + "metadata": {}, + "outputs": [], + "source": [ + "target.disconnect()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d0081236", + "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.10.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} |
