Making Scripts

Revision as of 09:56, 21 February 2018 by Fheubach (Talk | contribs) (added cleanup)

Revision as of 09:56, 21 February 2018 by Fheubach (Talk | contribs) (added cleanup)

For the older V3.x tools, see V3:Making_Scripts

Scripting with ChipWhisperer as a 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 during AES encryption 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()

# cleanup the connection to the target and scope
scope.dis()
target.dis()