Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
SamKarkache committed Apr 18, 2024
2 parents 4cf2935 + 706ed83 commit a07fe46
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 28 deletions.
17 changes: 13 additions & 4 deletions Testing/CWScopeTesting.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import time

import numpy as np

from WPI_SCA_LIBRARY.CWScope import *

firmware_path = "C:\\Users\\samka\\PycharmProjects\\MQP\\SCLA_API_MQP\\WPI_SCA_LIBRARY\\firmware\\simpleserial-aes-CWLITEARM-SS_2_1.hex"
firmware_path = "C:\\Users\\samka\\PycharmProjects\\SCLA_API_MQP\\WPI_SCA_LIBRARY\\firmware\\simpleserial-aes-CWLITEARM-SS_2_1.hex"


def benchmark_capture_procedures():
Expand Down Expand Up @@ -40,11 +42,18 @@ def benchmark_capture_procedures():
plt.ylabel("Time (s)")
plt.show()

benchmark_capture_procedures()


def test_cw_to_file_format():
scope = CWScope(firmware_path, gain=25, num_samples=3000, offset=0, target_type=cw.targets.SimpleSerial2,
target_programmer=cw.programmers.STM32FProgrammer)
file = FileParent("AnotherFile", "C:\\Users\\samka\\PycharmProjects\\MQP\\SCLA_API_MQP\\", existing=True)

file = FileParent("AnotherFile", "C:\\Users\\samka\\PycharmProjects\\SCLA_API_MQP\\", existing=True)
scope.cw_to_file_framework(1000, file, "TestExperiment")


scope = CWScope(firmware_path, gain=25, num_samples=3000, offset=0, target_type=cw.targets.SimpleSerial2, target_programmer=cw.programmers.STM32FProgrammer)
ktp = cw.ktp.Basic()

# keys = [ktp.next()[0]] * 1000
# texts = [ktp.next()[1]] * 1000
# scope.standard_capture_traces(1000, keys, texts)
9 changes: 9 additions & 0 deletions Testing/cupyTesting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import cupy as np
import numpy as slow
import time

randomValues = np.random.random_sample((1000,1000))
start = time.time()
mean = np.mean(randomValues)
end = time.time()
print(start - end)
8 changes: 4 additions & 4 deletions WPI_SCA_LIBRARY/CWScope.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def disconnect(self):
self.target.dis()

def standard_capture_traces(self, num_traces: int,
experiment_keys: np.ndarray = None,
experiment_texts: np.ndarray = None,
experiment_keys: list = None,
experiment_texts: list = None,
fixed_key: bool = True,
fixed_pt: bool = False) -> (np.ndarray, np.ndarray, np.ndarray, np.ndarray):
"""
Expand Down Expand Up @@ -131,8 +131,8 @@ def standard_capture_traces(self, num_traces: int,

return traces, keys, texts, ciphertexts

def capture_traces_tvla(self, num_traces: int, group_a_keys: np.ndarray = None, group_a_texts: np.ndarray = None,
group_b_keys: np.ndarray = None, group_b_texts: np.ndarray = None,
def capture_traces_tvla(self, num_traces: int, group_a_keys: list = None, group_a_texts: list= None,
group_b_keys: list = None, group_b_texts: list = None,
ktp: any = cwtvla.ktp.FixedVRandomText()) -> (np.ndarray, np.ndarray):
"""
Captures fixed and random trace set needed for TVLA.
Expand Down
1 change: 0 additions & 1 deletion WPI_SCA_LIBRARY/DPA.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def calculate_dpa(traces, iv, order=1, key_guess=0, window_size_fma=5, num_of_tr
return cpa_output, guess_corr, guess

if order == 2:
traces = traces[:, 0:4000]
if num_of_traces != 0:
fma = calculate_window_averages(traces, window_size=window_size_fma)
traces = np.array(fma[0:num_of_traces])
Expand Down
19 changes: 9 additions & 10 deletions WPI_SCA_LIBRARY/LecroyScope.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LecroyScope(object):
def __init__(self, scope_ip='TCPIP0::192.168.1.79::inst0::INSTR'):
"""
Initialize LecroyScope object.
:param scope_ip: The scope IP of the Lecroy scope
:param scope_ip: The IP of the scope that you want to connect to
"""
self.scope = None
self.rm = None
Expand All @@ -25,13 +25,18 @@ def __init__(self, scope_ip='TCPIP0::192.168.1.79::inst0::INSTR'):
self.valid_trigger_states = ['AUTO', 'NORM', 'SINGLE', 'STOP']

def __del__(self):
"""
Close the scope on object deletion.
:return: None
"""
self.close()

def open(self, scope_ip, scope_timeout=5000):
"""
Opens a Lecroy scope using the PyVisa library
:param scope_ip: The ip of the Lecroy scope
:param scope_timeout: The amount of time to wait for the Lecroy until timeout
:return: None
"""
self.rm = visa.ResourceManager()
try:
Expand All @@ -47,7 +52,7 @@ def open(self, scope_ip, scope_timeout=5000):
def close(self):
"""
Closes the Lecroy scope using the PyVisa library
:return:
:return: None
"""
try:
if self.scope is not None:
Expand All @@ -66,6 +71,7 @@ def setup(self, v_div, timebase, samplerate, duration, v_offset, channel):
:param duration: the duration of capture
:param v_offset: the voltage offset for the measurement
:param channel: the channel to capture the traces on
:return: None
"""
if self.scope:
self.scope.write("{}:TRA ON".format(channel))
Expand All @@ -84,6 +90,7 @@ def set_trigger(self, delay, level, channel='C1'):
:param delay: the trigger delay
:param level: the trigger level
:param channel: the trigger channel
:return: None
"""
if self.scope is None:
self.open(self.scope_ip)
Expand Down Expand Up @@ -289,14 +296,6 @@ def capture_cw305(scope, target, num_of_samples=600, short=False, channel='C3',


def capture_nopt(scope, num_of_samples=600, short=False, channel='C3'):
"""
Captures traces with no input
:param scope: the configured LecroyScope object
:param num_of_samples: The number of samples to capture
:param short:
:param channel: The channel to collect traces on
:return: The resulting trace
"""
if scope.start_trigger() is not False:
if scope.wait_for_trigger() is False:
return
Expand Down
12 changes: 6 additions & 6 deletions doc/cwscope.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ higher-level API calls
:type num_traces: int
:param experiment_keys: A collection of keys to use for the capture of each trace. If not specified, the procedure
will use the cw basic key generation `key = cw.ktp.Basic()[0]`
:type experiment_keys: np.ndarray
:type experiment_keys: list
:param experiment_texts: A collection of texts to use for the capture of each trace. If not specified, the procedure
will use the cw basic plaintext generation `text = cw.ktp.Basic()[1]`
:type experiment_texts: np.ndarray
:type experiment_texts: list
:param fixed_key: Whether to use a fixed key for cw.ktp key generation. Ignored if a collection of keys are supplied.
:type fixed_key: bool
:param fixed_pt: Whether to use a fixed plaintext for cw.ktp text generation. Ignored if a collection of texts are supplied.
Expand All @@ -61,13 +61,13 @@ higher-level API calls
:param num_traces: The number of traces to capture for each set
:type num_traces: int
:param group_a_keys: An array of keys for group A
:type group_a_keys: np.ndarray
:type group_a_keys: list
:param group_a_texts: An array of texts for group A
:type group_a_texts: np.ndarray
:type group_a_texts: list
:param group_b_keys: An array of keys for group B
:type group_b_keys: np.ndarray
:type group_b_keys: list
:param group_b_texts: An array of texts for group B
:type group_b_texts: np.ndarray
:type group_b_texts: list
:param ktp: the key text pair algorithm, defaults to cwtvla.ktp.FixedVRandomText(). This is ignored if keys or texts
for group A and group B are provided.
:rtype: (np.ndarray, np.ndarray)
Expand Down
22 changes: 21 additions & 1 deletion doc/dpa.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
Differential Power Analysis (DPA)
================================
================================

.. method:: calculate_dpa(traces, iv, order=1, key_guess=0, window_size_fma=5, num_of_traces=0):

Unified differential power analysis method that has support for first and second order DPA.

:param traces: The power traces to be processed
:param iv: Intermediate algorithm values associated with the power traces
:param key_guess: The DPA key guess
:param window_size_fma: The window size of the moving average calculation
:param num_of_traces: The number of traces being processed
:returns: The result of the DPA calculation

.. method:: calculate_second_order_dpa_mem_efficient(traces, IV, window_width):

Efficient implementation of second order DPA

:param traces: The power traces to be processed.
:param IV: Intermediate algorithm values associated with the power traces
:param window_width: The window size of the moving average calculation
:returns: The result of the DPA calculation
4 changes: 3 additions & 1 deletion doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ Library Features
fileformat.rst
cwscope.rst
metrics.rst
leakagemodels.rst
leakagemodels.rst
dpa.rst
lecroy.rst
119 changes: 118 additions & 1 deletion doc/lecroy.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,119 @@
Lecroy Oscilloscope Interface
=============================
=============================

.. class:: LecroyScope

.. method:: __init__(self, scope_ip='TCPIP0::192.168.1.79::inst0::INSTR'):

Create a LecroyScope object

:param scope_ip: The IP of the scope that you want to connect to

.. method:: __del__(self):

Close the scope on object deletion.

:return: None

.. method:: open(self, scope_ip, scope_timeout=5000):

Opens a Lecroy scope using the PyVisa library

:param scope_ip: The ip of the Lecroy scope
:param scope_timeout: The amount of time to wait for the Lecroy until timeout
:return: None

.. method:: close(self):

Closes the Lecroy scope using the PyVisa library

:return: None

.. method:: setup(self, v_div, timebase, samplerate, duration, v_offset, channel):

Sets up the Lecroy scope for trace capture

:param v_div: voltage scale per division
:param timebase: the timescale for the scope
:param samplerate: the rate in which measurements are sampled
:param duration: the duration of capture
:param v_offset: the voltage offset for the measurement
:param channel: the channel to capture the traces on
:return: None

.. method:: set_trigger(self, delay, level, channel='C1'):

Set the trigger for the trace capture

:param delay: the trigger delay
:param level: the trigger level
:param channel: the trigger channel
:return: None


.. method:: start_trigger(self)

Tells the LecroyScope to start the trigger based on the parameters set in LecroyScope.set_trigger


.. method:: get_trigger(self):

Returns the trigger status

:return: A string representing the trigger status

.. method:: wait_for_trigger(self):

Waits for the Lecroy trigger

:return: True if successful, False if the trigger timeout

.. method:: get_channel(self, samples, short, channel='C3'):

Get the measurement data from the Lecroy from specified channel

:param samples: The number of samples to record
:param short:
:param channel: The channel to collect data from
:return: The data from the scope

.. method:: reset(self):

Resets the scope

.. method:: scope_setup(channel='C3', trig_channel='C1', num_of_samples=200, sample_rate=500E6, short=False, v_div=2.5E-3, trg_delay="0", trg_level="1.65V", v_offset='0'):

Higher level setup function. Sets up a Lecroy Scope object for power trace collection.

:param channel: the channel that records power trace measurements
:param trig_channel: the trigger channel
:param num_of_samples: the number of samples to capture
:param sample_rate: the rate in which samples are captured
:param short:
:param v_div: the voltage scale per division
:param trg_delay: the trigger delay
:param trg_level: the trigger level
:param v_offset: the voltage offset
:return: the fully configured scope object

.. method:: dut_setup(board="CW305", fpga_id='100t', bitfile=None)

Sets up a target board for trace capture
:param board: The DUT type (CW305 or pico)
:param fpga_id: the FPGA id for CW305 target
:param bitfile: the bitfile for CW305 target
:return: The configured target


.. method:: capture_cw305(scope, target, num_of_samples=600, short=False, channel='C3', plaintext=None, key=None):

Captures traces on the CW305 target board

:param scope: The configured LecroyScope object
:param target: The configured CW305 target board
:param num_of_samples: The number of samples to capture
:param short:
:param channel: The channel to collect traces on
:param plaintext: The plaintext for the encryption algorithm
:param key: The key for the encryption algorithm
:return: the recorded trace and algorithm output

0 comments on commit a07fe46

Please sign in to comment.