Making Scripts

Revision as of 10:44, 17 June 2016 by Adriel (Talk | contribs) (Basic)

Revision as of 10:44, 17 June 2016 by Adriel (Talk | contribs) (Basic)

Basic

User scripts allows partial (i.e.: setting up the environment) or total automation of the execution flow.

A basic script would look like this:

from chipwhisperer.common.scripts.base import UserScriptBase


class UserScript(UserScriptBase):
    _name = "ChipWhisperer-Lite: AES SimpleSerial on XMEGA"
    _description = "SimpleSerial with Standard Target for AES (XMEGA)"

    def __init__(self, api):
        super(UserScript, self).__init__(api)

    def run(self):
        #User commands here
        self.api.setParameter(['Generic Settings', 'Scope Module', 'ChipWhisperer/OpenADC'])
        self.api.setParameter(['Generic Settings', 'Target Module', 'Simple Serial'])
        self.api.setParameter(['Generic Settings', 'Trace Format', 'ChipWhisperer/Native'])
        self.api.setParameter(['Simple Serial', 'Connection', 'ChipWhisperer-Lite'])
        self.api.setParameter(['ChipWhisperer/OpenADC', 'Connection', 'ChipWhisperer-Lite'])
                
        self.api.connect()
        
        #Example of using a list to set parameters. Slightly easier to copy/paste in this format
        lstexample = [['CW Extra Settings', 'Trigger Pins', 'Target IO4 (Trigger Line)', True],
                      ['CW Extra Settings', 'Target IOn Pins', 'Target IO1', 'Serial RXD'],
                      ['CW Extra Settings', 'Target IOn Pins', 'Target IO2', 'Serial TXD'],
                      ['OpenADC', 'Clock Setup', 'CLKGEN Settings', 'Desired Frequency', 7370000.0],
                      ['CW Extra Settings', 'Target HS IO-Out', 'CLKGEN'],
                      ['OpenADC', 'Clock Setup', 'ADC Clock', 'Source', 'CLKGEN x4 via DCM'],
                      ['OpenADC', 'Trigger Setup', 'Total Samples', 3000],
                      ['OpenADC', 'Trigger Setup', 'Offset', 1250],
                      ['OpenADC', 'Gain Setting', 'Setting', 45],
                      ['OpenADC', 'Trigger Setup', 'Mode', 'rising edge'],
                      #Final step: make DCMs relock in case they are lost
                      ['OpenADC', 'Clock Setup', 'ADC Clock', 'Reset ADC DCM', None],
                      ]
        for cmd in lstexample: self.api.setParameter(cmd)
        
        #Let's only do a few traces
        self.api.setParameter(['Generic Settings', 'Acquisition Settings', 'Number of Traces', 50])
                      
        #The environment is already set, lets do our first capture
        self.api.capture1()

User scripts should inherit from UserScriptBase that specifies the run() method that is called when clicking it in the menu or pressing the attack button (in the analyzer tool).

The API is passed as an argument by the GUI throught the constructor in order to allow the script to "remote control" the existing section. A name and a description should also be specified.

Running from the Terminal

This step is needed if you want to run the script from the terminal. In this case, you don't need to use the GUI, the capture can be performed using only the API. I order to do it, add the following lines at the end of your script file:

if __name__ == '__main__':
    from chipwhisperer.common.api.CWCoreAPI import CWCoreAPI
    api = CWCoreAPI()               # Instantiate the API
    api.runScriptClass(UserScript)  # Run UserScript through the API

or if you want the GUI:

if __name__ == '__main__':
    from chipwhisperer.common.api.CWCoreAPI import CWCoreAPI
    import chipwhisperer.capture.ui.CWCaptureGUI as cwc       # Import the ChipWhispererCapture GUI
    from chipwhisperer.common.utils.parameter import Parameter
    app = cwc.makeApplication()  
    Parameter.usePyQtGraph = True 
    api = CWCoreAPI()               # Instantiate the API
    gui = cwc.CWAnalyzerGUI(api)    # Instantiate the GUI
    gui.show() 
    api.runScriptClass(UserScript)  # Run UserScript through the API

    app.exec_()

Adding new scripts=

New scripts can be added to the tool's menu automatically by saving it in its respective script folder:

  • chipwhisperer/software/chipwhisperer/capture/scripts
  • chipwhisperer/software/chipwhisperer/analyzer/scripts

These directories already have some examples which can be used as a reference to create new ones. Files put in these directories are scanned during the tool initialization and all plugin classes (subclasses of Plugin) in all public files (not starting with "_") are loaded.

Scripts auto-generated by the analyzer tool can also be executed standalone or saved in the scripts directory so it will show up in the next GUI execution.

Advanced

If you decide to run both tools in sequence, do as follows:

if __name__ == '__main__':
    from chipwhisperer.common.api.CWCoreAPI import CWCoreAPI
    import chipwhisperer.capture.ui.CWCaptureGUI as cwc
    import chipwhisperer.analyzer.ui.CWAnalyzerGUI as cwa
    from chipwhisperer.common.utils.parameter import Parameter
    app = cwc.makeApplication()
    Parameter.usePyQtGraph = True
    api = CWCoreAPI()             # Instantiate the API
    gui = cwc.CWCaptureGUI(api)   # Instantiate the Capture GUI
    gui.show()
    api.runScriptClass(Capture)
    gui.close()
    gui.reset()                   # Delete saved geometry settings in the Capture tool so it will not be used by the Analyzer

    gui = cwa.CWAnalyzerGUI(api)  # Instantiate the Analyzer GUI
    gui.show()
    api.runScriptClass(Attack)    # Run the script (default is the "run" method)

    app.exec_()