-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTargetsModule.py
121 lines (99 loc) · 4.67 KB
/
TargetsModule.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
#!/usr/bin/env python3
# 2nd-party imports.
import CommonModule
import MetadataModule
def Target():
'''https://github.com/uptane/asn1/blob/master/TargetsModule.asn1'''
filename = CommonModule.FILENAME_SIZE_IN_BYTES
length = CommonModule.LENGTH_SIZE_IN_BYTES
numberOfHashes = CommonModule.LENGTH_SIZE_IN_BYTES
hashes = CommonModule.Hashes()
target = filename + length + numberOfHashes + hashes
CommonModule.log('Target', target)
return target
def Custom(ecuIdentifier=False, hardwareIdentifier=False, releaseCounter=False):
custom = 0
if ecuIdentifier:
custom += CommonModule.IDENTIFIER_SIZE_IN_BYTES
if hardwareIdentifier:
custom += CommonModule.IDENTIFIER_SIZE_IN_BYTES
if releaseCounter:
custom += CommonModule.NATURAL_SIZE_IN_BYTES
CommonModule.log('Custom', custom)
return custom
def TargetAndCustom(custom=True):
target = Target()
if custom:
custom = Custom(ecuIdentifier=True, hardwareIdentifier=True,
releaseCounter=True)
else:
custom = 0
targetAndCustom = target + custom
CommonModule.log('TargetAndCustom', targetAndCustom)
return targetAndCustom
def Targets(num_of_targets, custom=True):
targets = num_of_targets * TargetAndCustom(custom=custom)
CommonModule.log('# of targets', num_of_targets, ' targets')
CommonModule.log('Targets', targets)
return targets
def MultiRoles(num_of_keys_per_delegation=1, num_of_roles_per_delegation=1):
rolename = CommonModule.FILENAME_SIZE_IN_BYTES
numberOfKeyids = CommonModule.LENGTH_SIZE_IN_BYTES
keyids = CommonModule.Keyids(num_of_keys_per_delegation)
threshold = CommonModule.THRESHOLD_SIZE_IN_BYTES
MultiRole = rolename + numberOfKeyids + keyids + threshold
roles = num_of_roles_per_delegation * MultiRole
CommonModule.log('MultiRoles', roles)
return roles
def PrioritizedPathsToRoles(num_of_delegations=1,
num_of_keys_per_delegation=1,
num_of_paths_per_delegation=1,
num_of_roles_per_delegation=1):
numberOfPaths = CommonModule.LENGTH_SIZE_IN_BYTES
numberOfRoles = CommonModule.LENGTH_SIZE_IN_BYTES
# FIXME: No need for a whole byte to represent a BOOLEAN, but since we are
# currently fundamentally dealing with bytes, this will do for now. At least
# it overestimates rather than underestimate.
terminating = CommonModule.TAG_SIZE_IN_BYTES
paths = CommonModule.Paths(num_of_paths_per_delegation)
roles = MultiRoles(num_of_keys_per_delegation=num_of_keys_per_delegation,
num_of_roles_per_delegation=num_of_roles_per_delegation)
PathsToRoles = numberOfPaths + paths + numberOfRoles + roles + terminating
CommonModule.log('PathsToRoles', PathsToRoles)
delegations = num_of_delegations * PathsToRoles
CommonModule.log('PrioritizedPathsToRoles', delegations)
return delegations
def TargetsDelegations(num_of_delegations=1,
num_of_keys_per_delegation=1,
num_of_paths_per_delegation=1,
num_of_roles_per_delegation=1):
numberOfKeys = CommonModule.LENGTH_SIZE_IN_BYTES
numberOfDelegations = CommonModule.LENGTH_SIZE_IN_BYTES
num_of_keys = num_of_delegations * num_of_keys_per_delegation
keys = CommonModule.PublicKeys(num_of_keys=num_of_keys)
delegations = PrioritizedPathsToRoles(num_of_delegations=num_of_delegations,
num_of_keys_per_delegation=num_of_keys_per_delegation,
num_of_paths_per_delegation=num_of_paths_per_delegation,
num_of_roles_per_delegation=num_of_roles_per_delegation)
targetsDelegations = numberOfKeys + keys + numberOfDelegations + delegations
CommonModule.log('TargetsDelegations', targetsDelegations)
return targetsDelegations
def TargetsMetadata(custom=True,
num_of_keys=1,
num_of_targets=1,
num_of_delegations=0,
num_of_keys_per_delegation=1,
num_of_paths_per_delegation=1,
num_of_roles_per_delegation=1):
numberOfTargets = CommonModule.NATURAL_SIZE_IN_BYTES
targets = Targets(num_of_targets, custom=custom)
if num_of_delegations:
delegations = TargetsDelegations(num_of_delegations=num_of_delegations,
num_of_keys_per_delegation=num_of_keys_per_delegation,
num_of_paths_per_delegation=num_of_paths_per_delegation,
num_of_roles_per_delegation=num_of_roles_per_delegation)
else:
delegations = 0
targetsMetadata = numberOfTargets + targets + delegations
CommonModule.log('targetsMetadata', targetsMetadata)
return MetadataModule.Metadata(targetsMetadata, num_of_keys=num_of_keys)