As of August 2020 the site you are on (wiki.newae.com) is deprecated, and content is now at rtfm.newae.com.

MATLAB Control of CW-Lite

From ChipWhisperer Wiki
Revision as of 09:35, 1 March 2018 by Fheubach (Talk | contribs) (Adding a V4 version of interfacing chipwhisperer with matlab)

Jump to: navigation, search

For the older V3.x tools, see V3:MATLAB Control of CW-Lite

The following is designed to demonstrate how to control the CW-Lite directly from MATLAB.

About MATLAB to Python Interface

This system relies on the Mathworks Python interface, which is built in as of R2014B. See MathWorks Reference Page for more details.

The code is currently held in a separate repository at [1]. You'll have to clone that repository (or use the download link inside of GITHub) to copy the files into your own local working directory.

Python/ChipWhisperer Setup

You will need to first install Python 2.7 + ChipWhisperer. Note you'll need match the type of Python to your MATLAB install (i.e., 64-bit Python if using 64-bit MATLAB). Specific steps follow:

  1. Download WinPython 2.7.10.3, most likely you will require the 64-bit version (most recent MATLAB installs will be 64-bit).
  2. Run the installer - it will actually just extract this somewhere, I suggest to use a location such as c:\WinPython-64bit-2.7.10.3 rather than the default which will just install in a subdirectory of whereever you downloaded the installer to.
  3. Run the 'WinPython Command Prompt' - this will be in the directory you installed WinPython to.
  4. Run pip install chipwhisperer in the WinPython prompt which should install ChipWhisperer.
  5. Plug in CW-Lite, when prompted for drivers you'll have to download the driver zip-file and extract that somewhere, then point the installer to this location.
  6. Once drivers are installed, you should be able to run the examples from the WinPython command prompt. To do run the command python to start Python interpreter, then run:
  >>> import chipwhisperer
  >>> chipwhisperer.capture_gui()
  1. This should open the capture GUI. From the 'Project' and 'Example Scripts' menu, select 'ChipWhisperer-Lite: AES SimpleSerial on XMEGA'.
  2. Hopefully you see some waveforms show up on the screen!

MATLAB Setup

MATLAB will need to be told about your Python location most likely. You can check this by seeing the value of the pyversion variable at the MATLAB command prompt:

 >> pyversion

        version: 
     executable: 
        library: 
           home: 
       isloaded: 0

This indicates it does not have a valid Python environment. Simply point to your Python binary:

 >> pyversion 'C:\WinPython-64bit-2.7.9.5\python-2.7.9.amd64\python.exe'

Where you will need to adjust the path for your local version/install. You can confirm with pyversion this worked. You shouldn't need to do any more setup now, it should remember this Python environment.

Running Examples

Since the new CW 4,0 API exposes the internals of chipwhisperer the easiest way to get access to the chipwhisperer module in MATLAB is to use this API. MATLAB seems to not find certain modules even if they are on the python path so just use pythons build in importlib, which MATLAB can find, letting python find the module for MATLAB. Running this command in MATLAB

 >> cw = py.importlib.import_module('chipwhisperer')

should give you access to the 4.0 API.

If this works it should print the information about the 'cw' object:

 cw = 

  Python module with properties:

             TargetTemplate: [1×1 py.type]
              createProject: [1×1 py.function]
                 cwhardware: [1×1 py.type]
                    project: [1×1 py.module]
                 getLastKey: [1×1 py.chipwhisperer.gui_only]
                      scope: [1×1 py.function]
                   hardware: [1×1 py.module]
                    capture: [1×1 py.module]
               analyzer_gui: [1×1 py.function]
                   analyzer: [1×1 py.module]
                        cwa: [1×1 py.module]
                  CWCoreAPI: [1×1 py.type]
                   cwtarget: [1×1 py.type]
                   captureN: [1×1 py.chipwhisperer.gui_only]
                   updateUI: [1×1 py.function]
                     common: [1×1 py.module]
                        gui: [1×1 py.type]
              getLastTextin: [1×1 py.chipwhisperer.gui_only]
      AcquisitionController: [1×1 py.classobj]
                   gui_only: [1×1 py.type]
     acquisition_controller: [1×1 py.function]
                capture_gui: [1×1 py.function]
                  Parameter: [1×1 py.type]
            getLastExpected: [1×1 py.chipwhisperer.gui_only]
                    auxList: [1×1 py.chipwhisperer.gui_only]
    AcqKeyTextPattern_Basic: [1×1 py.type]
     trace_container_native: [1×1 py.module]
                         os: [1×1 py.module]
              ScopeTemplate: [1×1 py.type]
             getLastTextout: [1×1 py.chipwhisperer.gui_only]
                     target: [1×1 py.function]
                openProject: [1×1 py.function]
                   BasicKtp: [1×1 py.type]
                        cwc: [1×1 py.module]
               cw_bytearray: [1×1 py.type]

    <module 'chipwhisperer' from 'c:\chipwhisperer\software\chipwhisperer\__init__.pyc'>

If this FAILS, you may need to simply unplug/replug the ChipWhisperer-Lite. You may have not closed the connection from the previous test. Note you can actually interact directly with the 'cw' as in Python, just as you would after running import chipwhisperer as cw in Python. So you can initialize a scope and a target like so:

  >> scope = cw.scope()
  >> target = cw.target(scope)

You can now write scripts in MATLAB similar to the scripts in Making_Scripts.

Usage Notes

Most of the capture parameters are set inside of the 'cwapi.py' Python file. For example sample rate is fixed at 4x the clock frequency, and the clock frequency is fixed at 7.37 MHz. However the sample offset from trigger & length of capture can be set on the call to 'cwconnect'. By default the CW-Lite starts recording at 1250 samples after the trigger (1250 / 4 = 312.5 target device clock cyles), and records for 3000 samples (3000 / 4 = 750 target device clock cycles). These are suitable for the example AES in C, but you may need to reduce the offset if running AES in assembly for example.

The ChipWhisperer-Lite has a maximum capture length of about 24000 samples. The maximum offset is 4294967294.