-
Notifications
You must be signed in to change notification settings - Fork 16
/
cvn.py
2476 lines (2184 loc) · 84.5 KB
/
cvn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Cryptogram Version Number (CVN) defines the following:
- ICC Master Key derivation method
- Application Cryptogram (ARQC, TC, ACC) Session Key derivation method
- Application Cryptogram (ARQC, TC, ACC) calculation method
- Authorisation Response Cryptogram Session Key derivation method
- Authorisation Response Cryptogram calculation method
- Secure Messaging Session Key derivation method
- Secure Messaging Integrity (MAC) data format and padding
- Secure Messaging Confidentiality encryption method and padding
CVN module combines application cryptogram generation, key derivation and
secure messaging in one class per CVN.
>>> import pyemv
>>> cvn18 = pyemv.cvn.VisaCVN18(
... iss_mk_ac=bytes.fromhex('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'),
... iss_mk_smi=bytes.fromhex('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB'),
... iss_mk_smc=bytes.fromhex('CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC'),
... pan='1234567890123456',
... psn='00')
>>> atc = bytes.fromhex('0FFF')
>>> arqc = cvn18.generate_ac(
... tag_9f02=bytes.fromhex('000000009999'),
... tag_9f03=bytes.fromhex('000000000000'),
... tag_9f1a=bytes.fromhex('0840'),
... tag_95=bytes.fromhex('8000048000'),
... tag_5f2a=bytes.fromhex('0840'),
... tag_9a=bytes.fromhex('991231'),
... tag_9c=bytes.fromhex('01'),
... tag_9f37=bytes.fromhex('52BF4585'),
... tag_82=bytes.fromhex('1800'),
... tag_9f36=atc,
... tag_9f10=bytes.fromhex('06011203A0B800'))
>>> arqc.hex().upper()
'769577B5ABE9FE62'
>>> arpc = cvn18.generate_arpc(
... tag_9f26=arqc,
... tag_9f36=atc,
... csu=bytes.fromhex('00000000'))
>>> arpc.hex().upper()
'76503F48'
>>> command_mac = cvn18.generate_command_mac(
... command_header=bytes.fromhex('8418000008'),
... tag_9f26=arqc,
... tag_9f36=atc)
>>> command_mac.hex().upper()
'B5CB29759F9C3919'
>>> pin_command = cvn18.generate_pin_change_command(
... pin='9999',
... tag_9f26=arqc,
... tag_9f36=atc)
>>> pin_command.hex().upper()
'84240002182DC7A061323BA62472BC5308BD291B5F665B3A927E60661E'
"""
import typing as _typing
from pyemv import ac as _ac
from pyemv import kd as _kd
from pyemv import sm as _sm
__all__ = [
"VisaCVN10",
"VisaCVN18",
"VisaCVN22",
"InteracCVN133",
"MasterCardCVN16",
"MasterCardCVN17",
"MasterCardCVN20",
"MasterCardCVN21",
]
class VisaCVN10:
"""Cryptogram Version Number (CVN) defines Card Authentication Method (CAM).
Visa Cryptogram Version Number (CVN) 10 defines the following:
- ICC Master Key derivation method = Option A
- Application Cryptogram (ARQC, TC, ACC) Session Key derivation method = None
- Application Cryptogram (ARQC, TC, ACC) calculation method = Visa
- Authorisation Response Cryptogram Session Key derivation method = None
- Authorisation Response Cryptogram calculation method = 1
- Secure Messaging Session Key derivation method = Visa
- Secure Messaging Integrity (MAC) data format and padding = Format 2,
padded with transaction data
- Secure Messaging Confidentiality encryption method and padding = Visa
Parameters
----------
iss_mk_ac : bytes
16-byte binary Issuer Master Key for Application Cryptography.
Has to be a valid DES key.
iss_mk_smi : bytes
16-byte binary Issuer Master Key for Issuer Script Integrity.
Has to be a valid DES key.
iss_mk_smc : bytes
16-byte binary Issuer Master Key for Issuer Script Confidentiality.
Has to be a valid DES key.
pan : bytes or str
ASCII Application Primary Account Number.
psn : bytes or str, optional
ASCII 2-digit PAN Sequence Number (default 00).
Attributes
----------
icc_mk_ac : bytes
16-byte binary ICC Master Key for Application Cryptography.
icc_mk_smi : bytes
16-byte binary ICC Master Key for Issuer Script Integrity.
icc_mk_smc : bytes
16-byte binary ICC Master Key for Issuer Script Confidentiality.
"""
def __init__(
self,
iss_mk_ac: bytes,
iss_mk_smi: bytes,
iss_mk_smc: bytes,
pan: _typing.Union[bytes, str],
psn: _typing.Optional[_typing.Union[bytes, str]] = None,
) -> None:
# Derive AC, SMI, and SMC ICC Master Keys for a new card
# using option A.
psn = psn or "00"
self.icc_mk_ac = _kd.derive_icc_mk_a(iss_mk_ac, pan, psn)
self.icc_mk_smi = _kd.derive_icc_mk_a(iss_mk_smi, pan, psn)
self.icc_mk_smc = _kd.derive_icc_mk_a(iss_mk_smc, pan, psn)
def _derive_sk_ac_none(self) -> bytes:
"""Derive Application Cryptogram Session Key.
Use ICC Master Key, since Session Key is not applicable.
Returns
-------
sk_ac : bytes
16-byte binary Session Key for Application Cryptogram.
Has to be a valid DES key.
"""
return self.icc_mk_ac
def generate_ac(
self,
tag_9f02: bytes,
tag_9f03: bytes,
tag_9f1a: bytes,
tag_95: bytes,
tag_5f2a: bytes,
tag_9a: bytes,
tag_9c: bytes,
tag_9f37: bytes,
tag_82: bytes,
tag_9f36: bytes,
cvr: bytes,
) -> bytes:
"""Generate Application Cryptogram. Same process for
- Authorisation Request Cryptogram (ARQC)
- Transaction Cryptogram (TC)
- Application Authentication Cryptogram (AAC)
Parameters
----------
tag_9f02 : bytes
Binary data from tag 9F02 (Amount, Authorized).
tag_9f03 : bytes
Binary data from tag 9F03 (Amount, Other).
tag_9f1a : bytes
Binary data from tag 9F1A (Terminal Country Code).
tag_95 : bytes
Binary data from tag 95 (Terminal Verification Results).
tag_5f2a : bytes
Binary data from tag 5F2A (Transaction Currency Code).
tag_9a : bytes
Binary data from tag 9A (Transaction Date).
tag_9c : bytes
Binary Data from tag 9C (Transaction Type).
tag_9f37 : bytes
Binary data from tag 9F37 (Unpredictable Number).
tag_82 : bytes
Binary data from tag 82 (Application Interchange Profile).
tag_9f36 : bytes
Binary data from tag 9F36 (Application Transaction Counter).
cvr : bytes
4 bytes of binary Card Verification Results extracted from
9F10 (Issuer Application Data).
Returns
-------
ac : bytes
Returns binary 8-byte cryptogram (ARQC, TC, AAC).
"""
return _ac.generate_ac(
self._derive_sk_ac_none(),
tag_9f02
+ tag_9f03
+ tag_9f1a
+ tag_95
+ tag_5f2a
+ tag_9a
+ tag_9c
+ tag_9f37
+ tag_82
+ tag_9f36
+ cvr,
_ac.PaddingType.VISA,
8,
)
def generate_arpc(self, tag_9f26: bytes, arpc_rc: bytes) -> bytes:
"""Generate Authorisation Response Cryptogram (ARPC) using method 1.
Method for the generation of a 8-byte ARPC consists of applying
ISO/IEC 9797-1 MAC algorithm 3 to:
- 8-byte binary ARQC
- 2-byte binary ARPC response code
Parameters
----------
tag_9f26 : bytes
Binary data from tag 9F26 (Authorisation Request Cryptogram).
arpc_rc : bytes
Binary 2-byte ARPC response code.
Returns
-------
arpc : bytes
Returns binary 8-byte Authorisation Response Cryptogram (ARPC).
The resulting issuer authentication data (tag 91) is:
91 || Len || ARPC || ARPC-RC
"""
return _ac.generate_arpc_1(self._derive_sk_ac_none(), tag_9f26, arpc_rc)
def _derive_sk_sm_visa(self, icc_mk_sm: bytes, tag_9f36: bytes) -> bytes:
"""Derive Secure Messaging Session Key using Visa method.
Parameters
----------
icc_mk_sm : bytes
16-byte binary ICC Master Key for Secure Messaging.
Has to be a valid DES key.
tag_9f36 : bytes
Binary data from tag 9F36 (Application Transaction Counter).
Returns
-------
sk_sm : bytes
16-byte binary Session Key for Secure Messaging.
Has to be a valid DES key.
"""
return _kd.derive_visa_sm_sk(icc_mk_sm, tag_9f36)
def generate_command_mac(
self,
command_header: bytes,
tag_9f26: bytes,
tag_9f36: bytes,
command_data: bytes = b"",
) -> bytes:
r"""Message Authentication Code (MAC) for Secure Messaging Integrity.
Parameters
----------
command_header : bytes
Binary command header, such as \x84\x24\x00\x00\x08 for PIN unblock.
tag_9f26 : bytes
Binary data from tag 9F26 (Authorisation Request Cryptogram).
tag_9f36 : bytes
Binary data from tag 9F36 (Application Transaction Counter).
command_data : bytes, optional
Binary command data, e.g. PUT DATA or PIN block.
Returns
-------
mac : bytes
Binary 8-byte command MAC.
"""
return _sm.generate_command_mac(
self._derive_sk_sm_visa(self.icc_mk_smi, tag_9f36),
command_header + tag_9f36 + tag_9f26 + command_data,
8,
)
def encrypt_command_data(self, command_data: bytes, tag_9f36: bytes) -> bytes:
"""Command Data Encryption for Secure Messaging Confidentiality.
Parameters
----------
command_data : bytes
Binary command data, e.g. PUT DATA or PIN block.
tag_9f36 : bytes
Binary data from tag 9F36 (Application Transaction Counter).
Returns
-------
encrypted_command_data : bytes
Binary encrypted command command data. Then the resulting command is:
Header || Encrypted Data || MAC
"""
return _sm.encrypt_command_data(
self._derive_sk_sm_visa(self.icc_mk_smc, tag_9f36),
command_data,
_sm.EncryptionType.VISA,
)
def generate_pin_change_command(
self,
pin: _typing.Union[bytes, str],
tag_9f26: bytes,
tag_9f36: bytes,
current_pin: _typing.Optional[_typing.Union[bytes, str]] = None,
) -> bytes:
"""Generate a PIN change command with encrypted PIN block and MAC.
Parameters
----------
pin : bytes or str
New ASCII Personal Identification Number.
tag_9f26 : bytes
Binary data from tag 9F26 (Authorisation Request Cryptogram).
tag_9f36 : bytes
Binary data from tag 9F36 (Application Transaction Counter).
current_pin : bytes or str, optional
Current ASCII Personal Identification Number (optional). If present
VIS PIN block is generated using current PIN.
Returns
-------
pin_change_command : bytes
Binary PIN change command with encrypted PIN block and MAC.
"""
enc_pin_block = self.encrypt_command_data(
_sm.format_vis_pin_block(self.icc_mk_ac, pin, current_pin), tag_9f36
)
if current_pin is None:
command_header = b"\x84\x24\x00\x02\x18"
else:
command_header = b"\x84\x24\x00\x01\x18"
return (
command_header
+ enc_pin_block
+ self.generate_command_mac(
command_header, tag_9f26, tag_9f36, enc_pin_block
)
)
class VisaCVN18:
"""Cryptogram Version Number (CVN) defines Card Authentication Method (CAM).
Visa Cryptogram Version Number (CVN) 18 defines the following:
- ICC Master Key derivation method = Option B
- Application Cryptogram (ARQC, TC, ACC) Session Key derivation method = Common
- Application Cryptogram (ARQC, TC, ACC) calculation method = EMV
- Authorisation Response Cryptogram Session Key derivation method = Common
- Authorisation Response Cryptogram calculation method = 2
- Secure Messaging Session Key derivation method = Visa
- Secure Messaging Integrity (MAC) data format and padding = Format 2,
padded with transaction data
- Secure Messaging Confidentiality encryption method and padding = Visa
Parameters
----------
iss_mk_ac : bytes
16-byte binary Issuer Master Key for Application Cryptography.
Has to be a valid DES key.
iss_mk_smi : bytes
16-byte binary Issuer Master Key for Issuer Script Integrity.
Has to be a valid DES key.
iss_mk_smc : bytes
16-byte binary Issuer Master Key for Issuer Script Confidentiality.
Has to be a valid DES key.
pan : bytes or str
ASCII Application Primary Account Number.
psn : bytes or str, optional
ASCII 2-digit PAN Sequence Number (default 00).
Attributes
----------
icc_mk_ac : bytes
16-byte binary ICC Master Key for Application Cryptography.
icc_mk_smi : bytes
16-byte binary ICC Master Key for Issuer Script Integrity.
icc_mk_smc : bytes
16-byte binary ICC Master Key for Issuer Script Confidentiality.
"""
def __init__(
self,
iss_mk_ac: bytes,
iss_mk_smi: bytes,
iss_mk_smc: bytes,
pan: _typing.Union[bytes, str],
psn: _typing.Optional[_typing.Union[bytes, str]] = None,
) -> None:
# Derive AC, SMI, and SMC ICC Master Keys for a new card
# using option B.
psn = psn or "00"
self.icc_mk_ac = _kd.derive_icc_mk_b(iss_mk_ac, pan, psn)
self.icc_mk_smi = _kd.derive_icc_mk_b(iss_mk_smi, pan, psn)
self.icc_mk_smc = _kd.derive_icc_mk_b(iss_mk_smc, pan, psn)
def _derive_sk_ac_common(self, tag_9f36: bytes) -> bytes:
"""Derive Application Cryptogram Session Key using EMV Common method.
Parameters
----------
tag_9f36 : bytes
Binary data from tag 9F36 (Application Transaction Counter).
Returns
-------
sk_ac : bytes
16-byte binary Session Key for Application Cryptogram.
Has to be a valid DES key.
"""
return _kd.derive_common_sk(self.icc_mk_ac, tag_9f36 + b"\x00" * 6)
def generate_ac(
self,
tag_9f02: bytes,
tag_9f03: bytes,
tag_9f1a: bytes,
tag_95: bytes,
tag_5f2a: bytes,
tag_9a: bytes,
tag_9c: bytes,
tag_9f37: bytes,
tag_82: bytes,
tag_9f36: bytes,
tag_9f10: bytes,
) -> bytes:
"""Generate Application Cryptogram. Same process for
- Authorisation Request Cryptogram (ARQC)
- Transaction Cryptogram (TC)
- Application Authentication Cryptogram (AAC)
Parameters
----------
tag_9f02 : bytes
Binary data from tag 9F02 (Amount, Authorized).
tag_9f03 : bytes
Binary data from tag 9F03 (Amount, Other).
tag_9f1a : bytes
Binary data from tag 9F1A (Terminal Country Code).
tag_95 : bytes
Binary data from tag 95 (Terminal Verification Results).
tag_5f2a : bytes
Binary data from tag 5F2A (Transaction Currency Code).
tag_9a : bytes
Binary data from tag 9A (Transaction Date).
tag_9c : bytes
Binary Data from tag 9C (Transaction Type).
tag_9f37 : bytes
Binary data from tag 9F37 (Unpredictable Number).
tag_82 : bytes
Binary data from tag 82 (Application Interchange Profile).
tag_9f36 : bytes
Binary data from tag 9F36 (Application Transaction Counter).
tag_9f10 : bytes
Binary data from tag 9F10 (Issuer Application Data).
Returns
-------
ac : bytes
Returns binary 8-byte cryptogram (ARQC, TC, AAC).
"""
return _ac.generate_ac(
self._derive_sk_ac_common(tag_9f36),
tag_9f02
+ tag_9f03
+ tag_9f1a
+ tag_95
+ tag_5f2a
+ tag_9a
+ tag_9c
+ tag_9f37
+ tag_82
+ tag_9f36
+ tag_9f10,
_ac.PaddingType.EMV,
8,
)
def generate_arpc(
self,
tag_9f26: bytes,
tag_9f36: bytes,
csu: bytes,
proprietary_auth_data: _typing.Optional[bytes] = None,
) -> bytes:
"""Generate Authorisation Response Cryptogram (ARPC) using method 2.
Method for the generation of a 4-byte ARPC consists of applying
ISO/IEC 9797-1 MAC algorithm 3 to:
- 8-byte binary ARQC
- 4-byte binary Card Status Update (CSU)
- 0-8 byte binary Proprietary Authentication Data
Parameters
----------
tag_9f26 : bytes
Binary data from tag 9F26 (Authorisation Request Cryptogram).
tag_9f36 : bytes
Binary data from tag 9F36 (Application Transaction Counter).
csu : bytes
Binary 4-byte Card Status Update (CSU).
prop_auth_data : bytes, optional
Binary 0-8 byte Proprietary Authentication Data.
Returns
-------
arpc : bytes
Returns binary 4-byte Authorisation Response Cryptogram (ARPC).
The resulting issuer authentication data (tag 91) is:
91 || Len || ARPC || CSU || { Proprietary Authentication Data }
"""
return _ac.generate_arpc_2(
self._derive_sk_ac_common(tag_9f36),
tag_9f26,
csu,
proprietary_auth_data,
)
def _derive_sk_sm_visa(self, icc_mk_sm: bytes, tag_9f36: bytes) -> bytes:
"""Derive Secure Messaging Session Key using Visa method.
Parameters
----------
icc_mk_sm : bytes
16-byte binary ICC Master Key for Secure Messaging.
Has to be a valid DES key.
tag_9f36 : bytes
Binary data from tag 9F36 (Application Transaction Counter).
Returns
-------
sk_sm : bytes
16-byte binary Session Key for Secure Messaging.
Has to be a valid DES key.
"""
return _kd.derive_visa_sm_sk(icc_mk_sm, tag_9f36)
def generate_command_mac(
self,
command_header: bytes,
tag_9f26: bytes,
tag_9f36: bytes,
command_data: bytes = b"",
) -> bytes:
r"""Message Authentication Code (MAC) for Secure Messaging Integrity.
Parameters
----------
command_header : bytes
Binary command header, such as \x84\x24\x00\x00\x08 for PIN unblock.
tag_9f26 : bytes
Binary data from tag 9F26 (Authorisation Request Cryptogram).
tag_9f36 : bytes
Binary data from tag 9F36 (Application Transaction Counter).
command_data : bytes, optional
Binary command data, e.g. PUT DATA or PIN block.
Returns
-------
mac : bytes
Binary 8-byte command MAC.
"""
return _sm.generate_command_mac(
self._derive_sk_sm_visa(self.icc_mk_smi, tag_9f36),
command_header + tag_9f36 + tag_9f26 + command_data,
8,
)
def encrypt_command_data(self, command_data: bytes, tag_9f36: bytes) -> bytes:
"""Command Data Encryption for Secure Messaging Confidentiality.
Parameters
----------
command_data : bytes
Binary command data, e.g. PUT DATA or PIN block.
tag_9f36 : bytes
Binary data from tag 9F36 (Application Transaction Counter).
Returns
-------
encrypted_command_data : bytes
Binary encrypted command command data. Then the resulting command is:
Header || Encrypted Data || MAC
"""
return _sm.encrypt_command_data(
self._derive_sk_sm_visa(self.icc_mk_smc, tag_9f36),
command_data,
_sm.EncryptionType.VISA,
)
def generate_pin_change_command(
self,
pin: _typing.Union[bytes, str],
tag_9f26: bytes,
tag_9f36: bytes,
current_pin: _typing.Optional[_typing.Union[bytes, str]] = None,
) -> bytes:
"""Generate a PIN change command with encrypted PIN block and MAC.
Parameters
----------
pin : bytes or str
New ASCII Personal Identification Number.
tag_9f26 : bytes
Binary data from tag 9F26 (Authorisation Request Cryptogram).
tag_9f36 : bytes
Binary data from tag 9F36 (Application Transaction Counter).
current_pin : bytes or str, optional
Current ASCII Personal Identification Number (optional). If present
VIS PIN block is generated using current PIN.
Returns
-------
pin_change_command : bytes
Binary PIN change command with encrypted PIN block and MAC.
"""
enc_pin_block = self.encrypt_command_data(
_sm.format_vis_pin_block(self.icc_mk_ac, pin, current_pin), tag_9f36
)
if current_pin is None:
command_header = b"\x84\x24\x00\x02\x18"
else:
command_header = b"\x84\x24\x00\x01\x18"
return (
command_header
+ enc_pin_block
+ self.generate_command_mac(
command_header, tag_9f26, tag_9f36, enc_pin_block
)
)
class VisaCVN22:
"""Cryptogram Version Number (CVN) defines Card Authentication Method (CAM).
Visa Cryptogram Version Number (CVN) 18 defines the following:
- ICC Master Key derivation method = Option B
- Application Cryptogram (ARQC, TC, ACC) Session Key derivation method = Common
- Application Cryptogram (ARQC, TC, ACC) calculation method = EMV
- Authorisation Response Cryptogram Session Key derivation method = Common
- Authorisation Response Cryptogram calculation method = 2
- Secure Messaging Session Key derivation method = Common
- Secure Messaging Integrity (MAC) data format and padding = Format 2,
padded with transaction data
- Secure Messaging Confidentiality encryption method and padding = Visa
Parameters
----------
iss_mk_ac : bytes
16-byte binary Issuer Master Key for Application Cryptography.
Has to be a valid DES key.
iss_mk_smi : bytes
16-byte binary Issuer Master Key for Issuer Script Integrity.
Has to be a valid DES key.
iss_mk_smc : bytes
16-byte binary Issuer Master Key for Issuer Script Confidentiality.
Has to be a valid DES key.
pan : bytes or str
ASCII Application Primary Account Number.
psn : bytes or str, optional
ASCII 2-digit PAN Sequence Number (default 00).
Attributes
----------
icc_mk_ac : bytes
16-byte binary ICC Master Key for Application Cryptography.
icc_mk_smi : bytes
16-byte binary ICC Master Key for Issuer Script Integrity.
icc_mk_smc : bytes
16-byte binary ICC Master Key for Issuer Script Confidentiality.
"""
def __init__(
self,
iss_mk_ac: bytes,
iss_mk_smi: bytes,
iss_mk_smc: bytes,
pan: _typing.Union[bytes, str],
psn: _typing.Optional[_typing.Union[bytes, str]] = None,
) -> None:
# Derive AC, SMI, and SMC ICC Master Keys for a new card
# using option B.
psn = psn or "00"
self.icc_mk_ac = _kd.derive_icc_mk_b(iss_mk_ac, pan, psn)
self.icc_mk_smi = _kd.derive_icc_mk_b(iss_mk_smi, pan, psn)
self.icc_mk_smc = _kd.derive_icc_mk_b(iss_mk_smc, pan, psn)
def _derive_sk_ac_common(self, tag_9f36: bytes) -> bytes:
"""Derive Application Cryptogram Session Key using EMV Common method.
Parameters
----------
tag_9f36 : bytes
Binary data from tag 9F36 (Application Transaction Counter).
Returns
-------
sk_ac : bytes
16-byte binary Session Key for Application Cryptogram.
Has to be a valid DES key.
"""
return _kd.derive_common_sk(self.icc_mk_ac, tag_9f36 + b"\x00" * 6)
def generate_ac(
self,
tag_9f02: bytes,
tag_9f03: bytes,
tag_9f1a: bytes,
tag_95: bytes,
tag_5f2a: bytes,
tag_9a: bytes,
tag_9c: bytes,
tag_9f37: bytes,
tag_82: bytes,
tag_9f36: bytes,
tag_9f10: bytes,
) -> bytes:
"""Generate Application Cryptogram. Same process for
- Authorisation Request Cryptogram (ARQC)
- Transaction Cryptogram (TC)
- Application Authentication Cryptogram (AAC)
Parameters
----------
tag_9f02 : bytes
Binary data from tag 9F02 (Amount, Authorized).
tag_9f03 : bytes
Binary data from tag 9F03 (Amount, Other).
tag_9f1a : bytes
Binary data from tag 9F1A (Terminal Country Code).
tag_95 : bytes
Binary data from tag 95 (Terminal Verification Results).
tag_5f2a : bytes
Binary data from tag 5F2A (Transaction Currency Code).
tag_9a : bytes
Binary data from tag 9A (Transaction Date).
tag_9c : bytes
Binary Data from tag 9C (Transaction Type).
tag_9f37 : bytes
Binary data from tag 9F37 (Unpredictable Number).
tag_82 : bytes
Binary data from tag 82 (Application Interchange Profile).
tag_9f36 : bytes
Binary data from tag 9F36 (Application Transaction Counter).
tag_9f10 : bytes
Binary data from tag 9F10 (Issuer Application Data).
Returns
-------
ac : bytes
Returns binary 8-byte cryptogram (ARQC, TC, AAC).
"""
return _ac.generate_ac(
self._derive_sk_ac_common(tag_9f36),
tag_9f02
+ tag_9f03
+ tag_9f1a
+ tag_95
+ tag_5f2a
+ tag_9a
+ tag_9c
+ tag_9f37
+ tag_82
+ tag_9f36
+ tag_9f10,
_ac.PaddingType.EMV,
8,
)
def generate_arpc(
self,
tag_9f26: bytes,
tag_9f36: bytes,
csu: bytes,
proprietary_auth_data: _typing.Optional[bytes] = None,
) -> bytes:
"""Generate Authorisation Response Cryptogram (ARPC) using method 2.
Method for the generation of a 4-byte ARPC consists of applying
ISO/IEC 9797-1 MAC algorithm 3 to:
- 8-byte binary ARQC
- 4-byte binary Card Status Update (CSU)
- 0-8 byte binary Proprietary Authentication Data
Parameters
----------
tag_9f26 : bytes
Binary data from tag 9F26 (Authorisation Request Cryptogram).
tag_9f36 : bytes
Binary data from tag 9F36 (Application Transaction Counter).
csu : bytes
Binary 4-byte Card Status Update (CSU).
prop_auth_data : bytes, optional
Binary 0-8 byte Proprietary Authentication Data.
Returns
-------
arpc : bytes
Returns binary 4-byte Authorisation Response Cryptogram (ARPC).
The resulting issuer authentication data (tag 91) is:
91 || Len || ARPC || CSU || { Proprietary Authentication Data }
"""
return _ac.generate_arpc_2(
self._derive_sk_ac_common(tag_9f36),
tag_9f26,
csu,
proprietary_auth_data,
)
def _derive_sk_sm_common(self, icc_mk_sm: bytes, tag_9f26: bytes) -> bytes:
"""Derive Secure Messaging Session Key using Common method.
Parameters
----------
icc_mk_sm : bytes
16-byte binary ICC Master Key for Secure Messaging.
Has to be a valid DES key.
tag_9f26 : bytes
Binary data from tag 9F26 (Authorisation Request Cryptogram).
Returns
-------
sk_sm : bytes
16-byte binary Session Key for Secure Messaging.
Has to be a valid DES key.
"""
return _kd.derive_common_sk(icc_mk_sm, tag_9f26)
def generate_command_mac(
self,
command_header: bytes,
tag_9f26: bytes,
tag_9f36: bytes,
command_data: bytes = b"",
) -> bytes:
r"""Message Authentication Code (MAC) for Secure Messaging Integrity.
Parameters
----------
command_header : bytes
Binary command header, such as \x84\x24\x00\x00\x08 for PIN unblock.
tag_9f26 : bytes
Binary data from tag 9F26 (Authorisation Request Cryptogram).
tag_9f36 : bytes
Binary data from tag 9F36 (Application Transaction Counter).
command_data : bytes, optional
Binary command data, e.g. PUT DATA or PIN block.
Returns
-------
mac : bytes
Binary 8-byte command MAC.
"""
return _sm.generate_command_mac(
self._derive_sk_sm_common(self.icc_mk_smi, tag_9f26),
command_header + tag_9f36 + tag_9f26 + command_data,
8,
)
def encrypt_command_data(self, command_data: bytes, tag_9f26: bytes) -> bytes:
"""Command Data Encryption for Secure Messaging Confidentiality.
Parameters
----------
command_data : bytes
Binary command data, e.g. PUT DATA or PIN block.
tag_9f26 : bytes
Binary data from tag 9F26 (Authorisation Request Cryptogram).
Returns
-------
encrypted_command_data : bytes
Binary encrypted command command data. Then the resulting command is:
Header || Encrypted Data || MAC
"""
return _sm.encrypt_command_data(
self._derive_sk_sm_common(self.icc_mk_smc, tag_9f26),
command_data,
_sm.EncryptionType.VISA,
)
def generate_pin_change_command(
self,
pin: _typing.Union[bytes, str],
tag_9f26: bytes,
tag_9f36: bytes,
current_pin: _typing.Optional[_typing.Union[bytes, str]] = None,
) -> bytes:
"""Generate a PIN change command with encrypted PIN block and MAC.
Parameters
----------
pin : bytes or str
New ASCII Personal Identification Number.
tag_9f26 : bytes
Binary data from tag 9F26 (Authorisation Request Cryptogram).
tag_9f36 : bytes
Binary data from tag 9F36 (Application Transaction Counter).
current_pin : bytes or str, optional
Current ASCII Personal Identification Number (optional). If present
VIS PIN block is generated using current PIN.
Returns
-------
pin_change_command : bytes
Binary PIN change command with encrypted PIN block and MAC.
Notes
-----
This command generates Visa PIN block format when current PIN is
supplied and ISO 9564-1 format 2 PIN block when it's not supplied.
Some issuers may choose to use ISO 9564-1 format 1 PIN block when
current PIN is not supplied.
"""
if current_pin is None:
# Use standard EMV PIN block format if current PIN is not required
command_header = b"\x84\x24\x00\x02\x18"
enc_pin_block = self.encrypt_command_data(
_sm.format_iso9564_2_pin_block(pin), tag_9f26
)
else:
# Use legacy Visa PIN block format if current PIN is required
command_header = b"\x84\x24\x00\x01\x18"
enc_pin_block = self.encrypt_command_data(
_sm.format_vis_pin_block(self.icc_mk_ac, pin, current_pin), tag_9f26
)
return (
command_header
+ enc_pin_block
+ self.generate_command_mac(
command_header, tag_9f26, tag_9f36, enc_pin_block
)
)
class InteracCVN133:
"""Cryptogram Version Number (CVN) defines Card Authentication Method (CAM).
Interac Cryptogram Version Number (CVN) 133 defines the following:
- ICC Master Key derivation method = Option A
- Application Cryptogram (ARQC, TC, ACC) Session Key derivation method = MasterCard
- Application Cryptogram (ARQC, TC, ACC) calculation method = EMV
- Authorisation Response Cryptogram Session Key derivation method = MasterCard
- Authorisation Response Cryptogram calculation method = 1
- Secure Messaging Session Key derivation method = MasterCard
- Secure Messaging Integrity (MAC) data format and padding = Format 2,
not padded with transaction data
- Secure Messaging Confidentiality encryption method and padding = MasterCard
Parameters
----------
iss_mk_ac : bytes
16-byte binary Issuer Master Key for Application Cryptography.
Has to be a valid DES key.
iss_mk_smi : bytes
16-byte binary Issuer Master Key for Issuer Script Integrity.
Has to be a valid DES key.
iss_mk_smc : bytes
16-byte binary Issuer Master Key for Issuer Script Confidentiality.
Has to be a valid DES key.
pan : bytes or str
ASCII Application Primary Account Number.
psn : bytes or str, optional
ASCII 2-digit PAN Sequence Number (default 00).
Attributes
----------
icc_mk_ac : bytes
16-byte binary ICC Master Key for Application Cryptography.
icc_mk_smi : bytes
16-byte binary ICC Master Key for Issuer Script Integrity.
icc_mk_smc : bytes
16-byte binary ICC Master Key for Issuer Script Confidentiality.
"""