-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathconfig.py
More file actions
253 lines (208 loc) · 9.66 KB
/
config.py
File metadata and controls
253 lines (208 loc) · 9.66 KB
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
"""
Basic Configuration Common Use Cases
"""
# (c) 2015-2018 Microchip Technology Inc. and its subsidiaries.
#
# Subject to your compliance with these terms, you may use Microchip software
# and any derivatives exclusively with Microchip products. It is your
# responsibility to comply with third party license terms applicable to your
# use of third party software (including open source software) that may
# accompany Microchip software.
#
# THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
# EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
# WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
# PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT,
# SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE
# OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF
# MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE
# FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL
# LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED
# THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR
# THIS SOFTWARE.
from cryptoauthlib import *
from cryptoauthlib.device import *
from common import *
import time
import ctypes
import base64
_atsha204_config = bytearray.fromhex(
'C8 00 55 00 8F 80 80 A1 82 E0 C4 F4 84 00 A0 85'
'86 40 87 07 0F 00 C4 64 8A 7A 0B 8B 0C 4C DD 4D'
'C2 42 AF 8F FF 00 FF 00 FF 00 FF 00 FF 00 FF 00'
'FF 00 FF 00 FF FF FF FF FF FF FF FF FF FF FF FF'
'FF FF FF FF 00 00 55 55')
# Example configuration for ATECC508A minus the first 16 bytes which are fixed by the factory
_atecc508_config = bytearray.fromhex(
'B0 00 55 00 8F 20 C4 44 87 20 87 20 8F 0F C4 36'
'9F 0F 82 20 0F 0F C4 44 0F 0F 0F 0F 0F 0F 0F 0F'
'0F 0F 0F 0F FF FF FF FF 00 00 00 00 FF FF FF FF'
'00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF'
'FF FF FF FF 00 00 55 55 FF FF 00 00 00 00 00 00'
'33 00 1C 00 13 00 13 00 7C 00 1C 00 3C 00 33 00'
'3C 00 3C 00 3C 00 30 00 3C 00 3C 00 3C 00 30 00')
# Example configuration for ATECC608A minus the first 16 bytes which are fixed by the factory
_atecc608_config = bytearray.fromhex(
'6A 00 00 01 85 00 82 00 85 20 85 20 85 20 C6 46'
'8F 0F 9F 8F 0F 0F 8F 0F 0F 0F 0F 0F 0F 0F 0F 0F'
'0D 1F 0F 0F FF FF FF FF 00 00 00 00 FF FF FF FF'
'00 00 00 00 00 00 03 F7 00 69 76 00 00 00 00 00'
'00 00 00 00 00 00 55 55 FF FF 0E 60 00 00 00 00'
'53 00 53 00 73 00 73 00 73 00 38 00 7C 00 1C 00'
'3C 00 1A 00 3C 00 30 00 3C 00 30 00 12 00 30 00')
_configs = {'ATSHA204A': _atsha204_config,
'ATECC508A': _atecc508_config,
'ATECC608': _atecc608_config }
# Safe input if using python 2
try: input = raw_input
except NameError: pass
def configure_device(iface='hid', device='ecc', i2c_addr=None, keygen=True, **kwargs):
ATCA_SUCCESS = 0x00
# Loading cryptoauthlib(python specific)
load_cryptoauthlib()
# Get the target default config
cfg = eval('cfg_at{}a_{}_default()'.format(atca_names_map.get(device), atca_names_map.get(iface)))
# Set interface parameters
if kwargs is not None:
for k, v in kwargs.items():
icfg = getattr(cfg.cfg, 'atca{}'.format(iface))
setattr(icfg, k, int(v, 16))
# Basic Raspberry Pi I2C check
if 'bus' not in kwargs:
if 'i2c' == iface and check_if_rpi():
cfg.cfg.atcai2c.bus = 1
# Initialize the stack
assert atcab_init(cfg) == ATCA_SUCCESS
print('')
# Check device type
info = bytearray(4)
assert atcab_info(info) == ATCA_SUCCESS
dev_name = get_device_name(info)
dev_type = get_device_type_id(dev_name)
# Reinitialize if the device type doesn't match the default
if dev_type != cfg.devtype:
cfg.dev_type = dev_type
assert atcab_release() == ATCA_SUCCESS
time.sleep(1)
assert atcab_init(cfg) == ATCA_SUCCESS
# Request the Serial Number
serial_number = bytearray(9)
assert atcab_read_serial_number(serial_number) == ATCA_SUCCESS
print('\nSerial number: ')
print(pretty_print_hex(serial_number, indent=' '))
# Check the zone locks
print('\nReading the Lock Status')
is_locked = AtcaReference(False)
assert ATCA_SUCCESS == atcab_is_locked(0, is_locked)
config_zone_lock = bool(is_locked.value)
assert ATCA_SUCCESS == atcab_is_locked(1, is_locked)
data_zone_lock = bool(is_locked.value)
print(' Config Zone: {}'.format('Locked' if config_zone_lock else 'Unlocked'))
print(' Data Zone: {}'.format('Locked' if data_zone_lock else 'Unlocked'))
# Get Current I2C Address
print('\nGetting the I2C Address')
response = bytearray(4)
assert ATCA_SUCCESS == atcab_read_bytes_zone(0, 0, 16, response, 4)
print(' Current Address: {:02X}'.format(response[0]))
# Program the configuration zone
print('\nProgram Configuration')
if not config_zone_lock:
config = _configs.get(dev_name)
if config is None:
raise ValueError('Unknown Device Type: {}'.format(dev_type))
# Update with the target I2C Address
if i2c_addr is not None:
config[0] = i2c_addr
print('\n New Address: {:02X}'.format(config[0]))
ck590_i2c_addr = 0xC0 if dev_name != 'ATSHA204A' else 0xC8
if config[0] != ck590_i2c_addr:
print(' The AT88CK590 Kit does not support changing the I2C addresses of devices.')
print(' If you are not using an AT88CK590 kit you may continue without errors')
print(' otherwise exit and specify a compatible (0x{:02X}) address.'.format(ck590_i2c_addr))
if 'Y' != input(' Continue (Y/n): '):
exit(0)
print(' Programming {} Configuration'.format(dev_name))
# Write configuration
assert ATCA_SUCCESS == atcab_write_bytes_zone(0, 0, 16, config, len(config))
print(' Success')
# Verify Config Zone
print(' Verifying Configuration')
config_qa = bytearray(len(config))
atcab_read_bytes_zone(0, 0, 16, config_qa, len(config_qa))
if config_qa != config:
raise ValueError('Configuration read from the device does not match')
print(' Success')
print(' Locking Configuration')
assert ATCA_SUCCESS == atcab_lock_config_zone()
print(' Locked')
else:
print(' Locked, skipping')
# Check data zone lock
print('\nActivating Configuration')
if not data_zone_lock:
# Generate initial ECC key pairs, if applicable
key_gen(dev_name)
# Lock the data zone
assert ATCA_SUCCESS == atcab_lock_data_zone()
print(' Activated')
else:
print(' Already Active')
# Generate new keys
if keygen and data_zone_lock:
print('\nGenerating New Keys')
key_gen(dev_name)
atcab_release()
def key_gen(dev_name):
"""Reviews the configuration of a device and generates new random ECC key pairs for slots that allow it."""
ATCA_SUCCESS = 0x00
if 'ECC' not in dev_name:
return # SHA device, no keys to generate
# Read the device configuration
config_data = bytearray(128)
assert ATCA_SUCCESS == atcab_read_config_zone(config_data)
if dev_name == 'ATECC508A':
config = Atecc508aConfig.from_buffer(config_data)
elif dev_name == 'ATECC608':
config = Atecc608Config.from_buffer(config_data)
else:
raise ValueError('Unsupported device {}'.format(dev_name))
# Review all slot configurations and generate keys where possible
for slot in range(16):
if not config.KeyConfig[slot].Private:
continue # Not a private key
if config.LockValue != 0x55:
# Data zone is already locked, additional conditions apply
skip_msg = ' Skipping key pair generation in slot {}: '.format(slot)
if not config.SlotConfig[slot].WriteConfig & 0x02:
print(skip_msg + 'GenKey is disabled')
continue
if not config.SlotLocked & (1 << slot):
print(skip_msg + 'Slot has ben locked')
continue
if config.KeyConfig[slot].ReqAuth:
print(skip_msg + 'Slot requires authorization')
continue
if config.KeyConfig[slot].PersistentDisable:
print(skip_msg + 'Slot requires persistent latch')
continue
print(' Generating key pair in slot {}'.format(slot))
public_key = bytearray(64)
assert ATCA_SUCCESS == atcab_genkey(slot, public_key)
print(convert_ec_pub_to_pem(public_key))
if __name__ == '__main__':
parser = setup_example_runner(__file__)
parser.add_argument('--i2c', help='I2C Address (in hex)')
parser.add_argument('--gen', default=True, help='Generate new keys')
args = parser.parse_args()
if args.i2c is not None:
args.i2c = int(args.i2c, 16)
print('\n--- WARNING - THIS IS FOR DEMONSTRATION NOT FOR ACTUAL CONFIGURATION ---')
print('The configuration this script will program is for demonstration purposes only')
print('\nYou should use the Trust Platform Development Suite for device configuration')
print('\n https://www.microchip.com/en-us/products/security-ics/trust-platform#Getting%20Started')
print('\nWould you like to continue executing this script?')
if 'Y' != input(' Continue (Y/n): '):
exit(0)
print('\nConfiguring the device with an example configuration')
configure_device(args.iface, args.device, args.i2c, args.gen, **parse_interface_params(args.params))
print('\nDevice Successfully Configured')