As of August 2020 the site you are on (wiki.newae.com) is deprecated, and content is now at rtfm.newae.com. |
Difference between revisions of "Making Scripts"
From ChipWhisperer Wiki
(Added example of headless script) |
|||
Line 1: | Line 1: | ||
{{Warningbox|For the older V3.x tools, see [[V3:Making_Scripts]]}} | {{Warningbox|For the older V3.x tools, see [[V3:Making_Scripts]]}} | ||
− | + | == Scripting with ChipWhisperer as python module == | |
+ | When used without the GUI, the 4.0 API removes much of the high level abstractions so you can have more control over the capture process. It also answers questions like--when I capture a trace in what order are things happening? The following example scripts will give you a starting point when scripting with the ChipWhisperer tool. | ||
+ | === Perform Some Traces and get the results as Numpy array === | ||
+ | This script is an example of using the <code>chipwhisperer</code> module for capturing traces during AES encryption. <b>Make sure to have the correct firmware loaded on the target.</b> | ||
+ | <syntaxhighlight lang=python> | ||
+ | import time | ||
+ | |||
+ | import numpy as np | ||
+ | import chipwhisperer as cw | ||
+ | from chipwhisperer.capture.acq_patterns.basic import AcqKeyTextPattern_Basic | ||
+ | import matplotlib.pyplot as plt | ||
+ | |||
+ | scope = cw.scope() | ||
+ | target = cw.target(scope) | ||
+ | |||
+ | # setup scope parameters | ||
+ | scope.gain.gain = 45 | ||
+ | scope.adc.samples = 3000 | ||
+ | scope.adc.offset = 1250 | ||
+ | scope.adc.basic_mode = "rising_edge" | ||
+ | scope.clock.clkgen_freq = 7370000 | ||
+ | scope.clock.adc_src = "clkgen_x4" | ||
+ | scope.trigger.triggers = "tio4" | ||
+ | scope.io.tio1 = "serial_rx" | ||
+ | scope.io.tio2 = "serial_tx" | ||
+ | scope.io.hs2 = "clkgen" | ||
+ | |||
+ | ktp = AcqKeyTextPattern_Basic(target=target) | ||
+ | |||
+ | traces = [] | ||
+ | N = 50 # Number of traces | ||
+ | for i in range(N): | ||
+ | key, text = ktp.newPair() # manual creation of a key, text pair can be substituted here | ||
+ | |||
+ | target.reinit() | ||
+ | |||
+ | target.setModeEncrypt() # only does something for targets that support it | ||
+ | target.loadEncryptionKey(key) | ||
+ | target.loadInput(text) | ||
+ | |||
+ | # run aux stuff that should run before the scope arms here | ||
+ | |||
+ | scope.arm() | ||
+ | |||
+ | # run aux stuff that should run after the scope arms here | ||
+ | |||
+ | target.go() | ||
+ | timeout=50 | ||
+ | # wait for target to finish | ||
+ | while target.isDone() is False and timeout: | ||
+ | timeout -= 1 | ||
+ | time.sleep(0.01) | ||
+ | |||
+ | try: | ||
+ | ret = scope.capture() | ||
+ | if ret: | ||
+ | print('Timeout happened during acquisition') | ||
+ | except IOError as e: | ||
+ | print('IOError: %s' % str(e)) | ||
+ | |||
+ | # run aux stuff that should happen after trace here | ||
+ | |||
+ | traces.append(scope.getLastTrace()) | ||
+ | |||
+ | trace_array = np.asarray(traces) # if you prefer to work with numpy array for number crunching | ||
+ | |||
+ | # show an example trace | ||
+ | plt.plot(traces[0]) | ||
+ | plt.show() | ||
+ | </syntaxhighlight> |
Revision as of 09:32, 21 February 2018
For the older V3.x tools, see V3:Making_Scripts
Scripting with ChipWhisperer as python module
When used without the GUI, the 4.0 API removes much of the high level abstractions so you can have more control over the capture process. It also answers questions like--when I capture a trace in what order are things happening? The following example scripts will give you a starting point when scripting with the ChipWhisperer tool.
Perform Some Traces and get the results as Numpy array
This script is an example of using the chipwhisperer
module for capturing traces during AES encryption. Make sure to have the correct firmware loaded on the target.
import time
import numpy as np
import chipwhisperer as cw
from chipwhisperer.capture.acq_patterns.basic import AcqKeyTextPattern_Basic
import matplotlib.pyplot as plt
scope = cw.scope()
target = cw.target(scope)
# setup scope parameters
scope.gain.gain = 45
scope.adc.samples = 3000
scope.adc.offset = 1250
scope.adc.basic_mode = "rising_edge"
scope.clock.clkgen_freq = 7370000
scope.clock.adc_src = "clkgen_x4"
scope.trigger.triggers = "tio4"
scope.io.tio1 = "serial_rx"
scope.io.tio2 = "serial_tx"
scope.io.hs2 = "clkgen"
ktp = AcqKeyTextPattern_Basic(target=target)
traces = []
N = 50 # Number of traces
for i in range(N):
key, text = ktp.newPair() # manual creation of a key, text pair can be substituted here
target.reinit()
target.setModeEncrypt() # only does something for targets that support it
target.loadEncryptionKey(key)
target.loadInput(text)
# run aux stuff that should run before the scope arms here
scope.arm()
# run aux stuff that should run after the scope arms here
target.go()
timeout=50
# wait for target to finish
while target.isDone() is False and timeout:
timeout -= 1
time.sleep(0.01)
try:
ret = scope.capture()
if ret:
print('Timeout happened during acquisition')
except IOError as e:
print('IOError: %s' % str(e))
# run aux stuff that should happen after trace here
traces.append(scope.getLastTrace())
trace_array = np.asarray(traces) # if you prefer to work with numpy array for number crunching
# show an example trace
plt.plot(traces[0])
plt.show()