-
Notifications
You must be signed in to change notification settings - Fork 4
/
benchmark.py
executable file
·151 lines (107 loc) · 3.7 KB
/
benchmark.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Diverse utils for the Smartcard assignment.
Foo-Manroot - 2019
Contributors:
MaxPowell
j0sNET
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from crypto import wg10
from scard.commands import SmartCardCommands
from smartcard.CardConnectionObserver import CardConnectionObserver
from smartcard.CardType import AnyCardType
from smartcard.CardRequest import CardRequest
import sys
import timeit
from smartcard.util import toHexString
NO_COLOR = "\x1b[0m"
LT_RED = "\x1b[1;31m"
LT_BLUE = "\x1b[1;34m"
LT_CYAN = "\x1b[1;36m"
LT_GREEN = "\x1b[1;32m"
LT_YELLOW = "\x1b[1;33m"
END_COLOR = NO_COLOR
OK_COLOR = LT_GREEN
CMD_COLOR = LT_BLUE
ERROR_COLOR = LT_RED
INFO_COLOR = LT_CYAN
WARN_COLOR = LT_YELLOW
class SCardObserver (CardConnectionObserver):
"""
Little interpreter for the SmartCard events.
Modified version of
https://pyscard.sourceforge.io/pyscard-framework.html#a-simple-apdu-tracer-and-interpreter
"""
def update (self, cardconnection, ccevent):
# return
if ccevent.type == "connect":
print (INFO_COLOR
, "Connected to "
, cardconnection.getReader ()
, END_COLOR
)
elif ccevent.type == "command":
str = toHexString (ccevent.args [0])
print ('> ', str)
elif ccevent.type == "response":
if [] == ccevent.args[0]:
print ('< []'
, INFO_COLOR
, "%02X %02X" % tuple (ccevent.args [-2:])
, END_COLOR
)
else:
print ('<'
, OK_COLOR
, toHexString (ccevent.args [0])
, INFO_COLOR
, "%02X %02X" % tuple (ccevent.args [-2:])
, END_COLOR
)
class Sender ():
def __init__ (self):
"""TODO: Docstring for __init__ .
:f: TODO
:returns: TODO
"""
self.cmd = SmartCardCommands.INTERNAL_AUTHN ([0, 1, 2, 3, 4, 5, 6, 7])
def send (self):
"""TODO: Docstring for send .
:f: TODO
:returns: TODO
"""
sc_reader.connection.transmit (self.cmd)
if __name__ == "__main__":
print ("Trying to connect...")
# Connection
try:
cardtype = AnyCardType ()
cardrequest = CardRequest (timeout = 10, cardType = cardtype)
cardservice = cardrequest.waitforcard ()
observer = SCardObserver ()
# self.observer has been initialized when calling 'init ()'
cardservice.connection.addObserver (observer)
cardservice.connection.connect ()
sc_reader = cardservice
print ("Connected :)")
except Exception as e:
print ("ERROR: " + str (e))
sys.exit (1)
# Send 3000 commands (3 x 1000 repetitions) and measures time
times = 23
# times = 1000
setup = "from __main__ import s"
s = Sender ()
print ("Starting benchmark with {:d} iterations...".format (times))
print ("Benchmark result: " + str (timeit.repeat ("s.send ()", setup = setup, number = times)) )