Capture and Project API#
This page documents the included helper functions and classes for capturing, storing, and plotting traces.
Project#
The ChipWhisperer project is a simple way of storing your traces and related data. It’s a fairly thin wrapper over a Zarr group and so behaves a lot like one. To create a new Project:
import chipwhisperer as cw
proj = cw.Project()
proj.save("/path/to/proj") # end with .zip for zip file
You can also open an existing project:
proj = cw.open_project("/path/to/project")
- class chipwhisperer.common.project.Project(group=None, init_size=100, dtypes=['int16', 'uint8', 'uint8', 'uint8'], resize_func=<function _resize_func>, overwrite=False)#
ChipWhisperer project for holding traces and interacting with Analyzer.
To create a project in memory:
import chipwhisperer as cw proj = cw.Project() cont = cw.TraceContainer(trace, pt, ct, key) # pt, ct, key optional proj.append(cont) # internal arrays created here
To create a project on disk:
proj = cw.Project('path/to/project')
To save as a zip file:
proj.save('path/to/file.zip') # .zip suffix used to detect zip save
To access fields:
proj.traces proj.plaintexts proj.ciphertexts proj.keys proj.containers # returns TraceContainer
To open a saved project:
cw.open_project('/path/to/project')
This class is also printable as a string:
print(proj) # shows metadata such as len, size, available fields, etc
- Parameters:
group (zarr.Group, str, Optional) – If a zarr.Group, assume a zarr group with self.PROJ_PROPERTIES as attrs and self.DATA_NAMES as arrays. If str, assume a path to create a zarr group at. If None, put project in memory. Defaults to None
init_size (int, optional) – Number of elements to create for initial arrays. Does nothing if a zarr.Group is passed to group. Defaults to 100
dtypes (list, dict, optional) – Datatypes to use for arrays if otherwise unspecified. Defaults to self.DEFAULT_DTYPES
resize_func (func optional) – Function to use when resizing arrays. Defaults to _resize_func (size doubling)
overwrite (bool, optional) – Whether or not to overwrite a project is you want to create and one already exists at your path
- append(tracecontainer)#
Append a new TraceContainer to this project.
Automatically resizes project if not enough space is available. Will attempt to convert to a tracecontainer if a Tuple or List is passed
The fields of the TraceContainer must match the existing fields of this Project, meaning if this project has plaintexts, the TraceContainer must contain plaintext and vice versa.
- Parameters:
tracecontainer (List, Tuple, TraceContainer)
- property ciphertext_len: int | None#
Get the length of the ciphertexts, or None if there aren’t any
- property ciphertexts: NDArray | None#
Get all the ciphertexts in this project as a numpy array, or None if there aren’t any
If you want to access these as a zarr array, access via project._group
- extend(project)#
Extends this project with another project
Automatically resizes internal arrays if not enough space is available
The fields of project must match the existing fields of this Project, meaning if this project has plaintexts, project must contain plaintexts and vice versa.
- Parameters:
project (Project) – Project to extend this project with.
- property has_ciphertexts: bool#
True if this project has ciphertexts, false if not
- property has_keys: bool#
True if this project has keys, false if not
- property has_plaintexts: bool#
True if this project has plaintexts, false if not
- property keys: NDArray | None#
Get all the keys in this project as a numpy array, or None if there aren’t any
If you want to access these as a zarr array, access via project._group
- property keys_len: int | None#
Get the length of the keys, or None if there aren’t any
- property num_traces: int#
Get the number of traces in this project
- property plaintext_len: int | None#
Get the length of the plaintexts, or None if there aren’t any
- property plaintexts: NDArray | None#
Get all the plaintexts in this project as a numpy array, or None if there aren’t any
If you want to access these as a zarr array, access via project._group
- save(path, ftype=None, overwrite=False)#
Save this project to a zip file or zarr directory
If ftype is not specified (default), tries to determine save type via extension.
- Parameters:
path (Path, str) – Path to save project to
ftype (str, Optional) – Type to save project as. Can be None, ‘zarr’, ‘zip’, or other types supported by shutils.archive_export
- Returns:
A new project if saving as a zarr type, or None
- property size: int#
Get the number of traces that can be held in this project without resizing
Note that resizing is automatic and isn’t typically an issue for most users
- property trace_len: int#
Get the length (in samples) of the traces in this project
- property traces: NDArray#
Get all the traces in this project as a numpy array
If you want to access these as a zarr array, access via project._group
Capture#
Before capturing a trace make sure:
The scope has been connected.
The scope is configured.
The target has been connected.
The target is configured.
The target has been programmed with the correct firmware.
Once you completed these steps you can use the
capture_trace function to complete a
capture of one trace. There are some helper classes that generate types of
key text patterns for input to the
capture_trace function. The function
will return a TraceContainer.
- chipwhisperer.capture_trace(scope, target, plaintext, key=None, ack=True, poll_done=False, as_int=True, always_send_key=False)#
Capture a trace, sending plaintext and key
Does all individual steps needed to capture a trace (arming the scope sending the key/plaintext, getting the trace data back, etc.). Uses target.output_len as the length of the expected target reponse for simpleserial.
- Parameters:
scope (ScopeTemplate) – Scope object to use for capture.
target (TargetTemplate) – Target object to read/write text from.
plaintext (bytearray) – Plaintext to send to the target. Should be unencoded bytearray (will be converted to SimpleSerial when it’s sent). If None, don’t send plaintext.
key (bytearray, optional) – Key to send to target. Should be unencoded bytearray. If None, don’t send key. Defaults to None.
ack (bool, optional) – Check for ack when reading response from target. Defaults to True.
poll_done (bool, optional) – poll Husky to find out when it’s done capturing, instead of calculating the capture time based on the capture parameters. Useful for long trigger-based segmented captures. Can also result in slightly faster captures when the number of samples is high. Defaults to False. Supported by Husky only.
as_int (bool, optional) – If False, return trace as a float. Otherwise, return as an int.
always_send_key (bool, optional) – If True, always send key. Otherwise, only send if the key is different from the last one sent.
- Return type:
Any- Returns:
Traceor None if capture timed out.- Raises:
Warning or OSError – Error during capture.
Example
Capturing a trace:
import chipwhisperer as cw scope = cw.scope() scope.default_setup() target = cw.target() ktp = cw.ktp.Basic() key, pt = ktp.new_pair() trace = cw.capture_trace(scope, target, pt, key)
Added in version 5.1: Added to simplify trace capture.
Changed in version 5.2: Added ack parameter and use of target.output_len
Changed in version 5.6.1: Added poll_done parameter for Husky
Changed in version 6.1: Returns TraceContainer and make always_send_key and as_int default to True
TraceContainer#
This class is a simple way of grouping a power trace and related data. It’s the preferred
way of passing single traces around in ChipWhisperer and is returned by
capture_trace.
You can easily construct a TraceContainer by passing the relevant fields:
cont = cw.TraceContinaer(trace, plaintext, ciphertext, key)
- class chipwhisperer.common.project.TraceContainer(trace, plaintext=None, ciphertext=None, key=None, metadata=None)#
Added in version 7.0: Added Trace class.
Plotting#
- chipwhisperer.plot(*args, **kwargs)#
Get a plotting object for use in Jupyter.
Uses a Holoviews/Bokeh plot with a width of 800 and a height of 600. You must have Holoviews and Bokeh installed, as well as be working in a Jupyter environment.
args and kwargs are the same as a typical Holoviews plot.
Plotting a trace in a Jupyter environment:
import chipwhisperer as cw scope = cw.scope() ... trace = cw.capture_trace(scope, target, text, key) display(cw.plot(trace.wave))
- Returns:
A holoviews Curve object
Added in version 5.4.
Tip
If you wish to plot multiple datasets on the same graph, do this:
cw.plot(trace[0].wave) * cw.plot(trace[1].wave)
Key Text Patterns#
Currently ChipWhisperer supports a single class for generating key text pairs:
Basic#
- class chipwhisperer.capture.acq_patterns.basic.AcqKeyTextPattern_Basic#
Class for getting basic keys and plaintexts.
Basic usage:
import chipwhisperer as cw ktp = cw.ktp.Basic() key, text = ktp.next()
- property fixed_key#
Generate fixed key (True) or not (False).
- Getter:
Return True if using fixed key or False if not.
- Setter:
Set whether to use fixed key (True) or not (False).
Added in version 5.1: Added fixed_key property
- property fixed_text#
Generate fixed plaintext (True) or not (False).
- Getter:
Return True if using fixed plaintext or False if not.
- Setter:
Set whether to use fixed plaintext (True) or not (False).
Added in version 5.1: Added fixed_text property
- new_pair()#
Called when a new encryption pair is requested
- next()#
Returns the next key text pair
Updates last key and text
- Returns:
(key (bytearray), text (bytearray))
Added in version 5.1: Added next
- next_key()#
Returns the next key
Does not update text. If key is fixed, returns the same key as last time
- Returns:
key (bytearray)
Added in version 5.1: Added next_key
- next_text()#
Returns the next plaintext
Does not update key. If text is fixed, returns the same plaintext as last time
- Returns:
text (bytearray)
Added in version 5.1: Added next_text
This class is also available as a simple iterator:
N = 100
for key, text, n in cw.ktp.BasicIt(N):
pass