-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathemv.h
5810 lines (5104 loc) · 202 KB
/
emv.h
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
/* emv.h - EVM Contactless, implementation in morden C++
* Copyright 2019 Daniel Hu <[email protected]>
*
* This file contains the Common code (like TLV, dda/cda, message/queue, etc.) and Entry Point
* Kernel 2 (Mastercard) and Kernel 3 (Visa) are implemented in separate files
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, and the entire permission notice in its entirety,
* including the disclaimer of warranties.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
* WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*
*/
#ifndef EMV_H
#define EMV_H
#include "message_and_id.h"
#include "secure_allocator.h"
#include <algorithm>
#include <array>
#include <exception>
#include <functional>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>
namespace emv {
// disabling log at preprocessing time can save considerable space
#ifdef HAVE_LOG
#define pr_verbose(...) logger.verbose(__VA_ARGS__)
#define pr_debug(...) logger.debug(__VA_ARGS__)
#define pr_info(...) logger.info(__VA_ARGS__)
#define pr_warn(...) logger.warn(__VA_ARGS__)
#define pr_error(...) logger.error(__VA_ARGS__)
#else
#define pr_verbose(...)
#define pr_debug(...)
#define pr_info(...)
#define pr_warn(...)
#define pr_error(...)
#endif
void bignum_exp_modulus(const secure_vector& base,
const secure_vector& exponent,
const secure_vector& modulus,
secure_vector& result);
void compute_sha1(const uint8_t* data, size_t length, uint8_t* hash);
static constexpr std::array<uint8_t, 5> VISA_AID = {0xA0, 0x00, 0x00, 0x00, 0x03};
template <typename Dest, typename Src>
Dest narrow_cast(Src src) {
Dest d = static_cast<Dest>(src);
if (src != static_cast<Src>(d))
throw std::bad_cast();
return d;
};
static inline char nibble2hex(uint8_t b) {
if (b <= 9) {
return b + '0';
} else {
return b - 10 + 'A';
}
};
static inline uint8_t hex2nibble(char h) {
if (h >= '0' && h <= '9')
return h - '0';
if (h >= 'A' && h <= 'F')
return h - 'A' + 10;
return (h - 'a' + 10) & 0x0F;
};
static secure_string to_decimal(uint32_t num, int digits = 0)
{
secure_string ret{};
while (num || digits) {
char c = '0' + (num % 10);
ret.push_back(c);
num = num / 10;
if (digits)
digits--;
}
if (ret.size() == 0)
ret = secure_string{"0"};
std::reverse(ret.begin(), ret.end());
return ret;
};
static secure_string to_decimal(const secure_vector& v)
{
uint32_t ret = 0;
for (auto b : v) {
ret = ((ret) << 8) + b;
}
return to_decimal(ret);
};
static secure_vector to_bcd(const secure_vector& bin) {
uint32_t v = 0;
for (auto b : bin) {
v = ((v) << 8) + b;
};
secure_vector bcd{};
while (v) {
uint8_t d = v % 10;
v = v / 10;
d = d | ((v % 10) << 4);
v = v / 10;
bcd.push_back(d);
};
if (bcd.size() == 0)
bcd.resize(1);
std::reverse(bcd.begin(), bcd.end());
return bcd;
};
static secure_vector hex2vector(const std::string& hex)
{
secure_vector v(hex.size() / 2);
for (unsigned i = 0; i != v.size(); i++) {
uint8_t b = hex2nibble(hex[i << 1]);
b = (b << 4) | hex2nibble(hex[(i << 1) + 1]);
v[i] = b;
};
return v;
};
static secure_string vector2hex(const secure_vector& v)
{
secure_string hex{};
for (auto b : v) {
hex.push_back(nibble2hex(b >> 4));
hex.push_back(nibble2hex(b & 0x0F));
}
return hex;
};
// only useful for 4 bytes and below
static uint32_t vector2int(const secure_vector& v) {
uint32_t r = 0;
for (unsigned i = 0; i != v.size(); i++) {
r = (r << 8) | v[i];
}
return r;
};
static inline secure_string byte2hex(uint8_t b)
{
secure_string str{};
str.push_back(nibble2hex(b >> 4));
str.push_back(nibble2hex(b & 0x0F));
return str;
};
std::string to_hex(uint32_t i)
{
std::string hex;
uint32_t mask = 0xFF000000;
int shift = 24;
while (mask != 0) {
uint8_t b = (mask & i) >> shift;
if (b || hex.size()) {
hex += byte2hex(b);
}
mask >>= 8;
shift -= 8;
}
if (hex.size() == 0)
hex = "00";
return hex;
};
class Logger {
public:
enum class LEVEL : uint8_t {
error,
warn,
info,
debug,
verbose
};
Logger(LEVEL l = LEVEL::debug) : level{l} {};
template <typename... Args>
void verbose(Args&&... args) {
print(LEVEL::verbose, std::forward<Args>(args)...);
};
template <typename... Args>
void debug(Args&&... args) {
print(LEVEL::debug, std::forward<Args>(args)...);
};
template <typename... Args>
void info(Args&&... args) {
print(LEVEL::info, std::forward<Args>(args)...);
};
template <typename... Args>
void warn(Args&&... args) {
print(LEVEL::warn, std::forward<Args>(args)...);
};
template <typename... Args>
void error(Args&&... args) {
print(LEVEL::error, std::forward<Args>(args)...);
};
template <typename... Args>
void print(LEVEL l, Args&&... args) {
if (level >= l)
do_print(std::forward<Args>(args)...);
};
private:
void log(const std::string& msg);
void log(const char* str);
void log(const secure_string& msg)
{
std::string s(msg.begin(), msg.end());
log(s);
}
template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>>
void log(T i) {
log(std::to_string(i));
}
void log(const secure_vector& bytes) {
log(vector2hex(bytes));
};
template <typename T, typename... Args>
void do_print(T arg, Args&&... args) {
log(arg);
if constexpr (sizeof...(args) > 0) {
do_print(std::forward<Args>(args)...);
}
};
LEVEL level;
};
extern Logger logger;
Logger& operator<<(Logger& l, std::string const& msg)
{
l.info(msg);
return l;
}
Logger& operator<<(Logger& l, secure_string const& msg)
{
l.info(msg);
return l;
}
Logger& operator<<(Logger& l, const char* msg)
{
l.info(std::string(msg));
return l;
}
Logger& operator<<(Logger& l, int num) {
l.info(num);
return l;
}
Logger& operator<<(Logger& l, const secure_vector& bytes) {
l.info(bytes);
return l;
}
#define MAX_TAG_SIZE sizeof(uint32_t)
template <typename Iter>
bool tlv_get_tag(Iter& begin, Iter end, uint32_t& tag, uint8_t& tag_size) {
if (begin == end)
return false;
tag_size = 1;
tag = *begin++;
if ((tag & 0x1F) == 0x1F) {
do {
if (begin == end || tag_size++ > MAX_TAG_SIZE)
return false;
tag = (tag << 8) + *begin++;
} while (tag & 0x80);
}
return true;
}
template <typename Iter>
bool tlv_get_length(Iter& begin, Iter end, uint32_t& length) {
if (begin >= end)
return false;
length = *begin++;
if (length & 0x80) {
unsigned num = length & 0x7f;
length = 0;
while (num-- > 0) {
if (begin == end)
return false;
length = (length << 8) + *begin++;
}
}
return true;
}
secure_vector tag_in_bytes(uint32_t tag) {
secure_vector ret{};
uint8_t shift = 24;
uint32_t mask = 0xFF000000;
while (mask != 0 && (tag & mask) == 0) {
mask >>= 8;
shift -= 8;
}
while (mask != 0) {
uint8_t b = static_cast<uint8_t>((tag & mask) >> shift);
ret.push_back(b);
shift -= 8;
mask >>= 8;
};
return ret;
};
secure_vector make_tlv(uint32_t tag, const secure_vector& value){
secure_vector ret{};
uint8_t shift = 24;
uint32_t mask = 0xFF000000;
while (mask != 0 && (tag & mask) == 0) {
mask >>= 8;
shift -= 8;
}
while (mask != 0) {
uint8_t b = static_cast<uint8_t>((tag & mask) >> shift);
ret.push_back(b);
shift -= 8;
mask >>= 8;
};
uint32_t size = value.size();
if (size <= 0x7F) {
ret.push_back(static_cast<uint8_t>(value.size()));
} else {
shift = 24;
mask = 0xFF000000;
while (mask != 0 && (size & mask) == 0) {
mask >>= 8;
shift -= 8;
}
uint8_t num = (shift >> 3) + 1;
ret.push_back(static_cast<uint8_t>(0x80 | num));
while (mask != 0) {
ret.push_back(static_cast<uint8_t>((size & mask) >> shift));
shift -= 8;
mask >>= 8;
}
}
std::copy(value.begin(), value.end(), back_inserter(ret));
return ret;
};
template <typename Iter>
bool tlv_get(Iter begin, Iter& end, uint32_t& tag, uint8_t& tagSize, Iter& value) {
uint32_t length;
if (!tlv_get_tag(begin, end, tag, tagSize) ||
!tlv_get_length(begin, end, length) ||
begin + length > end)
return false;
value = begin;
end = begin + length;
return true;
}
static bool tag_is_constructed(uint32_t tag) {
uint8_t shift = 24;
uint32_t mask = 0xFF000000;
while (mask != 0 && (tag & mask) == 0) {
mask >>= 8;
shift -= 8;
}
uint8_t b = static_cast<uint8_t>((tag & mask) >> shift);
return ((b & 0x20) != 0);
};
static inline bool tag_is_primitive(uint32_t tag) {
return !tag_is_constructed(tag);
};
template <typename Iter, typename Visitor>
bool tlv_visit(Iter begin, Iter end, Visitor& v, bool recurse = true) {
uint32_t tag = 0;
uint8_t tagSize = 0;
Iter value;
while (begin < end) {
Iter _end = end;
if (*begin == 0x00) {
begin++;
continue;
}
if (!tlv_get(begin, _end, tag, tagSize, value))
return false;
// constructed or primitive type?
if (((tag >> ((tagSize - 1) * 8)) & 0x20) != 0) {
if (!v(tag, value, _end, true)) {
return false;
};
if (recurse && !tlv_visit(value, _end, v, recurse))
return false;
} else {
if (!v(tag, value, _end, false))
return false;
}
begin = _end;
}
return true;
}
enum class TAG_TYPE : uint8_t {
A,
AN,
ANS,
B,
N,
CN,
VAR
};
using TAG_PERM = uint8_t;
static constexpr uint8_t PERM_ACT = 0x01;
static constexpr uint8_t PERM_DET = 0x02;
static constexpr uint8_t PERM_RA = 0x04;
static constexpr uint8_t PERM_K = 0x08;
struct tag_info {
constexpr tag_info(uint32_t id, TAG_TYPE type, const char* desc, uint8_t minlen, uint8_t maxlen, TAG_PERM perm = PERM_K) : id{id}, type{type}, desc{desc}, minlen{minlen}, maxlen{maxlen}, perm{perm} {};
uint32_t id;
TAG_TYPE type;
const char* desc;
uint8_t minlen;
uint8_t maxlen;
TAG_PERM perm;
virtual bool validate(const secure_vector& value) const {
return value.size() >= minlen && value.size() <= maxlen;
};
virtual secure_vector DOL(const secure_vector& value, size_t length) const {
secure_vector ret(value);
ret.resize(length);
return ret;
}
virtual secure_string to_string(const secure_vector& v) const
{
secure_string ret{};
for (auto b : v) {
ret.push_back(nibble2hex(b >> 4));
ret.push_back(nibble2hex(b & 0x0F));
};
return ret;
};
bool has_permission(TAG_PERM permission) const {
return (perm & permission) != permission;
};
};
struct tag_hash {
size_t operator()(const tag_info* t) const {
return std::hash<uint32_t>()(t->id);
}
};
struct tag_eq {
bool operator()(const tag_info* t1, const tag_info* t2) const {
return t1->id == t2->id;
};
};
using tag_sets = std::unordered_set<const tag_info*, tag_hash, tag_eq, default_secure_allocator<const tag_info*>>;
struct tag_ans : tag_info {
constexpr tag_ans(int id, const char* desc, uint8_t minlen, uint8_t maxlen, TAG_PERM perm = PERM_K) : tag_info(id, TAG_TYPE::ANS, desc, minlen, maxlen, perm){};
virtual secure_string to_string(const secure_vector& value) const override
{
secure_string ret{};
for (auto b : value) {
ret.push_back(static_cast<char>(b));
}
return ret;
};
};
struct tag_an : tag_info {
constexpr tag_an(int id, const char* desc, uint8_t minlen, uint8_t maxlen, TAG_PERM perm = PERM_K) : tag_info(id, TAG_TYPE::ANS, desc, minlen, maxlen, perm){};
virtual secure_string to_string(const secure_vector& value) const override
{
secure_string ret{};
for (auto b : value) {
ret.push_back(static_cast<secure_string::value_type>(b));
}
return ret;
};
};
using numeric_value_type = uint32_t;
struct tag_n : tag_info {
constexpr tag_n(uint32_t id, const char* desc, uint8_t minlen, uint8_t maxlen, TAG_PERM perm = PERM_K) : tag_info{id, TAG_TYPE::N, desc, minlen, maxlen, perm} {};
// ignore leading '0'
secure_string to_string(const secure_vector& v) const override
{
secure_string ret{};
bool ignore_zero = true;
for (auto b : v) {
auto n = b >> 4;
if (n > 9)
break;
if (n != 0 || !ignore_zero) {
ret.push_back('0' + n);
ignore_zero = false;
}
n = b & 0x0F;
if (n > 9)
break;
if (n != 0 || !ignore_zero) {
ret.push_back('0' + n);
ignore_zero = false;
}
}
return ret;
}
secure_vector from_string(const secure_string& s) const
{
int size = (maxlen + 1) / 2;
secure_vector ret(size);
auto p = s.rbegin();
while (p != s.rend() && size-- > 0) {
uint8_t b = ((*p - '0') & 0x0F);
p++;
if (p != s.rend()) {
b |= ((*p - '0') & 0x0F) << 4;
p++;
}
ret[size] = b;
}
return ret;
}
virtual secure_vector DOL(const secure_vector& value, size_t length) const override {
secure_vector ret{};
if (value.size() > length) { // leftmost truncated
std::copy(value.begin() + (value.size() - length), value.end(), back_inserter(ret));
return ret;
} else { // leftmost padding
ret.resize(length - value.size());
std::copy(value.begin(), value.end(), back_inserter(ret));
}
return ret;
};
static bool is_numeric(unsigned char b) {
return b >= 0 && b <= 9;
}
static numeric_value_type to_numeric_value(const secure_vector& v) {
numeric_value_type ret = 0;
for (auto b : v) {
ret = 10 * ret + ((b >> 4) & 0x0F);
ret = 10 * ret + (b & 0x0F);
}
return ret;
}
virtual bool validate(const secure_vector& v) const override {
if (v.size() > static_cast<unsigned int>((maxlen + 1) / 2) ||
v.size() < static_cast<unsigned int>((minlen + 1) / 2))
return false;
// TODO check nlen other than full length
for (auto b : v) {
if (!is_numeric(b >> 4) || !is_numeric(b & 0x0F))
return false;
}
return true;
}
};
struct tag_date : tag_n {
constexpr tag_date(uint32_t id, const char* desc, TAG_PERM perm = PERM_K) : tag_n(id, desc, 6, 6, perm){};
secure_string to_string(const secure_vector& v) const override
{
return vector2hex(v);
}
};
struct tag_cn : tag_info {
constexpr tag_cn(uint32_t id, const char* desc, uint8_t minlen, uint8_t maxlen, TAG_PERM perm = PERM_K) : tag_info{id, TAG_TYPE::CN, desc, minlen, maxlen, perm} {};
// ignore trailing padding 'F' or whatever
secure_string to_string(const secure_vector& v) const override
{
secure_string ret{};
for (auto b : v) {
auto n = b >> 4;
if (n <= 9)
ret.push_back('0' + n);
else
break;
n = b & 0x0F;
if (n <= 9)
ret.push_back('0' + n);
else
break;
}
return ret;
}
secure_vector from_string(const secure_string& d)
{
return secure_vector();
}
virtual secure_vector DOL(const secure_vector& value, size_t length) const override {
secure_vector ret(value);
if (value.size() < length) {
for (unsigned i = 0; i != length - value.size(); i++) {
ret.push_back(0xFF);
}
return ret;
} else {
ret.resize(length);
};
return ret;
};
bool validate(const secure_vector& v) const override {
if (v.size() < minlen || v.size() > maxlen) {
return false;
};
// TODO check CN format
return true;
};
};
struct tag_b : tag_info {
constexpr tag_b(uint32_t id, const char* desc, uint8_t minlen, uint8_t maxlen, TAG_PERM perm = PERM_K) : tag_info{id, TAG_TYPE::B, desc, minlen, maxlen, perm} {};
};
struct tag_var : tag_info {
constexpr tag_var(uint32_t id, const char* desc, uint8_t minlen, uint8_t maxlen, TAG_PERM perm = PERM_K) : tag_info{id, TAG_TYPE::VAR, desc, minlen, maxlen, perm} {};
};
struct tag_bit_field {
unsigned char byte;
unsigned char bit;
const char* desc;
};
void v_set_bit(secure_vector& value, const tag_bit_field& pos) {
value[pos.byte - 1] |= (0x01 << (pos.bit - 1));
};
void v_clear_bit(secure_vector& value, const tag_bit_field& pos) {
value[pos.byte - 1] &= ~(0x01 << (pos.bit - 1));
};
int v_get_bit(secure_vector const& value, const tag_bit_field& pos) {
return value[pos.byte - 1] & (0x01 << (pos.bit - 1));
};
#define DECL_TAG_BIT(name, p1, p2) \
static constexpr tag_bit_field name = {p1, p2, #name}
static void dump(const secure_vector& v, const tag_bit_field* field) {
if ((v[field->byte - 1] & (0x01 << (field->bit - 1))) != 0) {
pr_debug("<", field->desc, "> ");
}
};
template <typename TAG>
static void dump(TAG& tag, const secure_vector& v) {
for (auto p : tag.all_bits) {
dump(v, p);
}
pr_debug("\n");
};
// called from inside TAG static definition
#define DUMP(v) \
do { \
for (auto _x : all_bits) { \
dump((v), _x); \
} \
} while (false);
#define DECL_TAG_N(name, desc, tag, MIN, MAX, ...) \
static constexpr char name##_desc[] = desc; \
struct TAG_##name : public tag_n { \
constexpr TAG_##name() : tag_n(tag, name##_desc, MIN, MAX, ##__VA_ARGS__){}; \
}; \
static constexpr TAG_##name name{};
#define DECL_TAG_DATE(name, desc, tag, ...) \
static constexpr char name##_desc[] = desc; \
struct TAG_##name : public tag_date { \
constexpr TAG_##name() : tag_date(tag, name##_desc, ##__VA_ARGS__){}; \
}; \
static constexpr TAG_##name name{};
#define DECL_TAG_CN(name, desc, tag, MIN, MAX, ...) \
static constexpr char name##_desc[] = desc; \
struct TAG_##name : public tag_cn { \
constexpr TAG_##name() : tag_cn(tag, name##_desc, MIN, MAX, ##__VA_ARGS__){}; \
}; \
static constexpr TAG_##name name{};
#define DECL_TAG_B(name, desc, tag, MIN, MAX, ...) \
static constexpr char name##_desc[] = desc; \
struct TAG_##name : public tag_b { \
constexpr TAG_##name() : tag_b(tag, name##_desc, MIN, MAX, ##__VA_ARGS__){}; \
}; \
static constexpr TAG_##name name{};
#define DECL_TAG_VAR(name, desc, tag, MIN, MAX, ...) \
static constexpr char name##_desc[] = desc; \
struct TAG_##name : public tag_var { \
constexpr TAG_##name() : tag_var(tag, name##_desc, MIN, MAX, ##__VA_ARGS__){}; \
}; \
static constexpr TAG_##name name{};
#define DECL_TAG_ANS(name, desc, tag, MIN, MAX, ...) \
static constexpr char name##_desc[] = desc; \
struct TAG_##name : public tag_ans { \
constexpr TAG_##name() : tag_ans(tag, name##_desc, MIN, MAX, ##__VA_ARGS__){}; \
}; \
static constexpr TAG_##name name{};
#define DECL_TAG_AN(name, desc, tag, MIN, MAX, ...) \
static constexpr char name##_desc[] = desc; \
struct TAG_##name : public tag_an { \
constexpr TAG_##name() : tag_an(tag, name##_desc, MIN, MAX, ##__VA_ARGS__){}; \
}; \
static constexpr TAG_##name name{};
static std::string to_string(OUTCOME_CVM cvm) {
switch (cvm) {
case OUTCOME_CVM::NO_CVM:
return std::string("NO_CVM");
case OUTCOME_CVM::SIGNATURE:
return std::string("SIGNATURE");
case OUTCOME_CVM::ONLINE_PIN:
return std::string("ONLINE_PIN");
case OUTCOME_CVM::CONF_CODE_VERIFIED:
return std::string("CONF_CODE_VERIFIED");
default:
break;
};
return std::string{};
};
enum class RESTART_POINT : uint8_t {
A = 0,
B = 1,
C = 2,
D = 3,
NA = 0x0F
};
static std::string to_string(RESTART_POINT start) {
switch (start) {
case RESTART_POINT::A:
return std::string("A");
case RESTART_POINT::B:
return std::string("B");
case RESTART_POINT::C:
return std::string("C");
case RESTART_POINT::D:
return std::string("D");
default:
break;
}
return std::string{};
};
enum class OUTCOME_TYPE : uint8_t {
APPROVED = 1,
DECLINED = 2,
ONLINE_REQUEST = 3,
END_APPLICATION = 4,
SELECT_NEXT = 5,
TRY_ANOTHER_INTERFACE = 6,
TRY_AGAIN = 7,
NA = 15
};
static std::string to_string(OUTCOME_TYPE outcome_type) {
switch (outcome_type) {
case OUTCOME_TYPE::APPROVED:
return std::string("APPROVED");
case OUTCOME_TYPE::DECLINED:
return std::string("DECLINED");
case OUTCOME_TYPE::ONLINE_REQUEST:
return std::string("ONLINE REQUEST");
case OUTCOME_TYPE::END_APPLICATION:
return std::string("END APPLICATION");
case OUTCOME_TYPE::SELECT_NEXT:
return std::string("SELECT NEXT");
case OUTCOME_TYPE::TRY_ANOTHER_INTERFACE:
return std::string("TRY ANOTHER INTERFACE");
case OUTCOME_TYPE::TRY_AGAIN:
return std::string("TRY AGAIN");
default:
break;
}
return std::string{};
};
struct ui_req_data {
ui_req_data() : hold_time(0) {
value_type = ui_value_id::NA;
}
ui_message_id ui_id;
ui_status_id status;
int hold_time;
std::string lang;
ui_value_id value_type;
std::string value;
std::string currency_code;
};
std::string to_string(ui_value_id id) {
switch (id) {
case ui_value_id::AMOUNT:
return std::string("AMOUNT");
case ui_value_id::BALANCE:
return std::string("BALANCE");
default:
break;
}
return std::string{};
};
std::string to_string(ui_message_id id) {
switch (id) {
case ui_message_id::APPROVED:
return std::string("Approved");
case ui_message_id::NOT_AUTHORIZED:
return std::string("Not Authorised");
case ui_message_id::ENTER_PIN:
return std::string("Please enter your PIN");
case ui_message_id::PROCESSING_ERR:
return std::string("Processing error");
case ui_message_id::REMOVE_CARD:
return std::string("Please remove card");
case ui_message_id::WELCOME:
return std::string("Welcome");
case ui_message_id::PRESENT_CARD:
return std::string("Present card");
case ui_message_id::PROCESSING:
return std::string("Processing");
case ui_message_id::READ_OK:
return std::string("Card read OK, Please remove card");
case ui_message_id::INSERT_OR_SWIPE:
return std::string("Please insert or swipe card");
case ui_message_id::PRESENT_ONE_ONLY:
return std::string("Please present one card only");
case ui_message_id::APPROVED_PLEASE_SIGN:
return std::string("Approved Please sign");
case ui_message_id::AUTHORISING:
return std::string("Authorising Please Wait");
case ui_message_id::INSERT_SWIPE_TRY_ANOTHER:
return std::string("Insert, swipe or try another card");
case ui_message_id::INSERT_CARD:
return std::string("Please insert card");
case ui_message_id::NO_MESSAGE:
return std::string("*** CLEAR ****");
case ui_message_id::CHECK_PHONE:
return std::string("See Phone for Instructions");
case ui_message_id::TRY_AGAIN:
return std::string("Present card again");
default:
break;
}
return std::string();
};
std::string to_string(ui_status_id id) {
switch (id) {
case ui_status_id::NOT_READY:
return std::string("Not Ready");
case ui_status_id::IDLE:
return std::string("Idle");
case ui_status_id::PRESENT_CARD:
return std::string("Ready to Read (Present Card)");
case ui_status_id::PROCESSING:
return std::string("Processing");
case ui_status_id::CARD_READ_OK:
return std::string("Card Read Successfully (Remove Card)");
case ui_status_id::PROCESSING_ERR:
return std::string("Processing Error");
case ui_status_id::CONTACTLESS_NOT_SATISFIED:
return std::string("Processing Error: Conditions for use of contactless not satisfied");
case ui_status_id::COLLISION_ERR:
return std::string("Processing Error: Contactless collision detected");
case ui_status_id::CARD_NOT_REMOVED:
return std::string("Card not removed from reader");
default:
break;
}
return std::string();
};
enum class OUTCOME_KERNEL_RESTART_COND : uint8_t {
EMV_DATA_AVAIL,
ANY,
NA
};
enum class L1_ERROR : uint8_t {
OK = 0x00,
TIME_OUT_ERROR = 0x01,
TRANSMISSION_ERROR = 0x02,
PROTOCOL_ERROR = 0x03