forked from Chaman-veteran/KyberCPA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_traces.py
59 lines (46 loc) · 1.86 KB
/
get_traces.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
"""
get_traces.py
Module to capture and optionaly save power traces from the ARM Cortex-M4.
To use it for capture only: python get_traces.py
To save the capture: python get_traces.py --save-traces y >> ciphertexts
"""
from mupq import platforms
import matplotlib.pyplot as plt
from json import dumps
from argparse import ArgumentParser
PATH_TO_FIRMWARE = 'bin/crypto_kem_kyber512_m4fspeed_test.hex'
"""
As we are using the segment mode of the CW, we let this note copied from CW documentation when using the segment mode:
#IMPORTANT - we now need to generate enough triggers such that scope.adc.samples * NUM_TRIGGERS > max_fifo_size
# If not the HW won't exit capture mode. In this example code we weill just call the function so
# many times.
For more information, see: https://github.com/newaetech/chipwhisperer-jupyter/blob/84a9feabcfe5f1b76e31c4ac7d96d00f2577685c/demos/Using%20Segmented%20Memory%20for%20Hardware%20AES%20(STM32F4).ipynb#L185
In our case (with a CW-Lite), max_fifo_size = 24400
"""
parser = ArgumentParser(description="Get traces from kyber512 implementation")
parser.add_argument(
"--save-traces",
dest='save_traces',
default=False,
)
parser.parse_args()
args = vars(parser.parse_args())
save_traces = bool(args.get('save_traces'))
cw = platforms.ChipWhisperer()
cw.scope.adc.samples = 1000 #200
cw.scope.adc.basic_mode = "rising_edge"
cw.scope.adc.fifo_fill_mode = "segment"
print(cw.run(PATH_TO_FIRMWARE))
cw.scope.capture_segmented()
power_traces = cw.scope.get_last_trace_segmented()
print(f'Captured {len(power_traces)} segments of {len(power_traces[0])} points each.')
plt.plot(power_traces[0])
plt.xlabel('Sample')
plt.ylabel('Power consumption')
plt.show()
cw.scope.dis()
cw.target.dis()
if save_traces:
with open('traces.log', 'a+') as f:
f.write(dumps(power_traces.tolist()))
f.write('\n')