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 "MATLAB Control of CW-Lite"

From ChipWhisperer Wiki
Jump to: navigation, search
(Added warning box)
(Adding a V4 version of interfacing chipwhisperer with matlab)
Line 45: Line 45:
  
 
== Running Examples ==
 
== 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
To run the examples, you will need to ensure you set the MATLAB working directory to the location of the .m script files. The following shows my example setup:
+
<syntaxhighlight lang=matlab>
 
+
  >> cw = py.importlib.import_module('chipwhisperer')
[[File:api_matlab_path.png]]
+
</syntaxhighlight>
 
+
should give you access to the 4.0 API.
The matlab files internally will reference a .py file in that same directory. You should be able to simply connect to the ChipWhisperer-Lite with the following:
+
 
+
  >> cw = cwconnect()
+
  
 
If this works it should print the information about the 'cw' object:
 
If this works it should print the information about the 'cw' object:
 
+
<syntaxhighlight>
 
  cw =  
 
  cw =  
 
  Python CWCoreAPI with properties:
 
 
                sigNewProject: [1x1 py.chipwhisperer.common.utils.util.Signal]
 
                    valid_aux: [1x1 py.collections.OrderedDict]
 
                      auxParam: [1x1 py.chipwhisperer.common.utils.parameter.Parameter]
 
              sigConnectStatus: [1x1 py.chipwhisperer.common.utils.util.Signal]
 
            sigNewTextResponse: [1x1 py.chipwhisperer.common.utils.util.Signal]
 
                valid_attacks: [1x1 py.collections.OrderedDict]
 
                  sigTraceDone: [1x1 py.chipwhisperer.common.utils.util.Signal]
 
                    traceParam: [1x1 py.chipwhisperer.common.utils.parameter.Parameter]
 
              executingScripts: [1x1 py.chipwhisperer.common.utils.util.Observable]
 
                    scopeParam: [1x1 py.chipwhisperer.common.utils.parameter.Parameter]
 
              sigCampaignStart: [1x1 py.chipwhisperer.common.utils.util.Signal]
 
              sigNewInputData: [1x1 py.chipwhisperer.common.utils.util.Signal]
 
                        params: [1x1 py.chipwhisperer.common.utils.parameter.Parameter]
 
                      settings: [1x1 py.chipwhisperer.common.api.settings.Settings]
 
            valid_acqPatterns: [1x1 py.collections.OrderedDict]
 
                  valid_scopes: [1x1 py.collections.OrderedDict]
 
                  targetParam: [1x1 py.chipwhisperer.common.utils.parameter.Parameter]
 
                  valid_traces: [1x1 py.collections.OrderedDict]
 
                valid_targets: [1x1 py.collections.OrderedDict]
 
              sigAttackChanged: [1x1 py.chipwhisperer.common.utils.util.Signal]
 
              sigCampaignDone: [1x1 py.chipwhisperer.common.utils.util.Signal]
 
    valid_preprocessingModules: [1x1 py.collections.OrderedDict]
 
 
    <chipwhisperer.common.api.CWCoreAPI.CWCoreAPI object at 0x000000013312FD30>
 
 
 
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' object as in Python. For example we can get information about the current scope module:
 
 
  >> cw.getScope()
 
 
But you probably just want to run an AES test. To do this, simply perform the following:
 
 
  [cipher, trace] = measure_AES(cw, [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15],[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]);
 
 
The measure_AES() function passes both the plaintext and key. In this example they are both set to the same array value. It returns the ciphertext resulting & the power trace.
 
 
You can plot this power trace with:
 
  
   plot(trace)
+
   Python module with properties:
  
[[File:matlab_plot.png|400px]]
+
            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]
  
You can run a number of measure_AES() calls with the same cw object without running cwconnect() again. Note if you want to reconnect, you will first need to run cw.disconnect() to close the USB connection.
+
    <module 'chipwhisperer' from 'c:\chipwhisperer\software\chipwhisperer\__init__.pyc'>
 +
</syntaxhighlight>
  
If you receive 'device in use' error messages, this is likely the problem. You can also simply unplug/replug the USB connection to force-close any existing connections.
+
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 <code> import chipwhisperer as cw </code> in Python. So you can initialize a scope and a target like so:
 +
<syntaxhighlight lang=matlab>
 +
  >> scope = cw.scope()
 +
  >> target = cw.target(scope)
 +
</syntaxhighlight>
 +
You can now write scripts in MATLAB similar to the scripts in [[Making_Scripts]].
  
 
== Usage Notes ==
 
== Usage Notes ==

Revision as of 08:35, 1 March 2018

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.