MyBinder

Smartcards

This notebook showcases how to use pyecsca to communicate with a smartcard target, either via a PCSC-compatible reader or a LEIA board. The target in this notebook is the ECTester applet running on a JavaCard.

But first, a showcase of the raw LEIA and PCSC APIs

[ ]:
from pyecsca.sca.target.leia import LEIATarget
from pyecsca.sca.target.PCSC import PCSCTarget
from smartleia import LEIA
from smartcard.System import readers

LEIA

For the following lines to work you need a LEIA board connected and a card inserted inside of it.

[ ]:
sl = LEIA()
leia = LEIATarget(sl)
[ ]:
leia.connect()
[ ]:
leia.atr.hex()
[ ]:
leia.select(bytes.fromhex("73696D706C656170706C6574"))
[ ]:
leia.disconnect()

PCSC

For the following lines to work you need a PCSC-compatible reader connected and a card inserted inside of it. On Linux you also likely need the PCSClite 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.

[ ]:
rs = readers()
reader = rs[0]
pcsc = PCSCTarget(reader)
[ ]:
pcsc.connect()
[ ]:
pcsc.atr.hex()
[ ]:
pcsc.select(bytes.fromhex("73696D706C656170706C6574"))
[ ]:
pcsc.disconnect()

ECTester

The following lines assume that the ECTester applet is installed on a card connected via a PCSC-compatible reader.

[ ]:
from pyecsca.sca.target.ectester import ECTesterTargetPCSC, KeyAgreementEnum
[ ]:
rs = readers()
for reader in rs:
    if "Gemalto" in reader.name:
        break
target = ECTesterTargetPCSC(reader)
[ ]:
target.connect()
[ ]:
target.select_applet()
[ ]:
target.atr.hex()
[ ]:
target.allocate_ka(KeyAgreementEnum.ALG_EC_SVDP_DH_PLAIN)
[ ]:
target.info()
[ ]:
target.disconnect()
[ ]: