Skip to main content

A Python package for EMV cryptograhy in payment systems

Project description

PyEMV

PyPI Test coverage

This package provides methods for the generation of the Application Cryptograms (TC, ARQC, or AAC) generated by the ICC and the Authorisation Response Cryptogram (ARPC) generated by the issuer and verified by the ICC. In addition, this package supports secure messaging integrity and confidentiality.

Install:

pip install pyemv

PyEMV consists of the following modules:

  • emv.ac - Application Cryptogram support for ARQC, AAC, TC, and ARPC
  • emv.sm - Secure Messaging support for script command integrity and confidentiality
  • emv.kd - Key Derivation support for ICC master keys and session keys
  • emv.cvn - Cryptogram Version Number module to showcase how EMV security and key management concepts connect together

Examples

ICC Master Key derivation method A and B:

>>> from emv import kd
>>> iss_mk = bytes.fromhex('0123456789ABCDEFFEDCBA9876543210')
>>> pan = b'99012345678901234'
>>> psn = b'45'
>>> icc_mk_a = kd.icc_mkd_a(iss_mk, pan, psn)
>>> icc_mk_a.hex().upper()
'67F8292358083E5EA7AB7FDA58D53B6B'
>>> icc_mk_b = kd.icc_mkd_b(iss_mk, pan, psn)
>>> icc_mk_b.hex().upper()
'985EC4FD3EDF6162E31AF1C7D0543416'

Common Session Key derivation:

>>> r = bytes.fromhex('1234567890123456')
>>> sk = kd.common_skd(icc_mk_a, r)
>>> sk.hex().upper()
'29B33180E567CE38EA4CBC9D753B0E61'

Application Request Cryptogram generation:

>>> from emv import ac
>>> ac_data = bytes.fromhex('0123456789ABCDEF0123456789ABCDEF')
>>> arqc = ac.generate_ac(sk, ac_data)
>>> arqc.hex().upper()
'FA624250B008B59A'

Application Response Cryptogram generation method 1 and 2:

>>> arpc_rc = bytes.fromhex('0000')
>>> arpc = ac.generate_arpc_1(sk, arqc, arpc_rc)
>>> arpc.hex().upper()
'45D4255EEF10C920'
>>> csu = bytes.fromhex('00000000')
>>> arpc = ac.generate_arpc_2(sk, arqc, csu)
>>> arpc.hex().upper()
'CB56FA40'

Secure Messaging Integrity (MAC):

>>> from emv import sm
>>> sk_smi = bytes.fromhex('0123456789ABCDEFFEDCBA9876543210')
>>> command = bytes.fromhex('8424000008')
>>> mac = sm.generate_command_mac(sk_smi, command)
>>> mac.hex().upper()
'0BFFF5DF3FAA24E1'

Secure Messaging Confidentiality:

>>> sk_smc = bytes.fromhex('0123456789ABCDEFFEDCBA9876543210')
>>> command_data = bytes.fromhex('049999FFFFFFFFFF')
>>> enc_data = sm.encrypt_command_data(sk_smc, command_data,
...                                    sm.CommandDataEncryptionType.emv)
>>> enc_data.hex().upper()
'CBB927BDD83C49AC9CB8D2A8AB2AF4C9'

Contribute

PyEMV is hosted on GitHub.

Feel free to fork and send contributions over.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for pyemv, version 0.0a3
Filename, size File type Python version Upload date Hashes
Filename, size pyemv-0.0a3-py3-none-any.whl (17.0 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size pyemv-0.0a3.tar.gz (34.8 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page