-
Notifications
You must be signed in to change notification settings - Fork 4
/
measure_javacard.py
171 lines (137 loc) · 5.52 KB
/
measure_javacard.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
# MIT License
#
# Copyright (c) 2020-2024 SCRUTINY developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argparse
import os
import sys
import jsonpickle
from scrutiny import config
from scrutiny.device import Device, DeviceType
from scrutiny.javacard.toolwrappers.gppro import GPProInfo, GPProList
from scrutiny.javacard.toolwrappers.jcalgtest import JCAlgTestSupport, \
JCAlgTestPerformance, JCAlgTestVariable, JCAlgTestSupportExtended
from scrutiny.utils import isdir, errmsg
CFG_FILE = config.MeasureJavaCard.CFG_FILE
SPEED = config.MeasureJavaCard.SPEED
RISK = config.MeasureJavaCard.RISK
KNOWN_SUBTESTS = [
"gppro_info",
"gppro_list",
"jcalgtest_support",
"jcalgtest_support_extended",
"jcalgtest_performance",
"jcalgtest_variable"
]
def get_wrapper(test, card_name):
"""Creates tool wrapper by string definition from configuration"""
if test == "gppro_info":
return GPProInfo(card_name)
if test == "gppro_list":
return GPProList(card_name)
if test == "jcalgtest_support":
return JCAlgTestSupport(card_name)
if test == "jcalgtest_support_extended":
return JCAlgTestSupportExtended(card_name)
if test == "jcalgtest_performance":
return JCAlgTestPerformance(card_name)
if test == "jcalgtest_variable":
return JCAlgTestVariable(card_name)
return None
def prepare_results(card_name):
"""
Creates directory in results/
:param card_name: card name
:return: True on success
"""
dirname = "results/" + card_name
if isdir(dirname):
print(dirname, "already exists, skipping the creation.")
return True
try:
print("Creating", dirname + ".")
os.mkdir(dirname)
return True
except FileExistsError as ex:
return errmsg(dirname, "creating", ex)
def get_subtests(config_dict, configuration):
"""Returns subtests of specific configuration"""
subtests = []
for param in config_dict[configuration]:
if config_dict[configuration][param].lower() == "yes" and \
param in KNOWN_SUBTESTS:
subtests.append(param)
return subtests
def help_string(config_dict, config_section):
"""Generate help string for specific configuration"""
result = "Configuration: " + config_section + "\n"
if "speed" in config_dict[config_section]:
result += "Speed: " + config_dict[config_section]["speed"] + "\n" + \
SPEED.get(config_dict[config_section]["speed"], "")
if "risk" in config_dict[config_section]:
result += "Risk: " + config_dict[config_section]["risk"] + "\n" + \
RISK.get(config_dict[config_section]["risk"], "")
result += "Subtests:\n " +\
"\n ".join(get_subtests(config_dict, config_section))
return result
if __name__ == "__main__":
with open(CFG_FILE, "r") as f:
cf = jsonpickle.decode(f.read())
configurations = []
for section in cf:
configurations.append(section)
parser = argparse.ArgumentParser()
parser.add_argument("device_name",
help="the name of the device to be used")
parser.add_argument("-c", "--use-configuration",
metavar="cfg",
help="Configurations currently defined in "
+ CFG_FILE + ": " + " ".join(configurations)
)
parser.add_argument("-i", "--info-configuration",
metavar="cfg",
help="Prints information about configuration")
args = parser.parse_args()
if args.info_configuration:
if args.info_configuration in configurations:
print(help_string(cf, args.info_configuration))
sys.exit(0)
else:
print("Configuration", args.info_configuration,
"not define in " + CFG_FILE)
sys.exit(1)
if not args.use_configuration or \
args.use_configuration not in configurations:
print("Using default configuration.")
args.use_configuration = "default"
device_name = args.device_name.replace(" ", "_")
device = Device(device_name, DeviceType.JAVA_CARD)
prepare_results(device_name)
tool_wrappers = [
get_wrapper(test, device_name)
for test
in get_subtests(cf, args.use_configuration)
]
for tool in tool_wrappers:
tool.run()
device.add_modules(tool.parse())
with open("results/" + device_name + ".json", "w") as output:
output.write(str(device))
sys.exit(0)