I’m using CW capture rev 2. I want to do a template attack in Matlab. So, I captured 20000 power traces. Now I want to export them to Matlab for further processing. It should be noted that when capturing a lot of traces, we need to raise the number of segments, so I put this number to 100 to store 200 power traces in each segment.
My question is, how should I modify the native_to_matlab.py code to deal with a greater number of segments than 1?
Hy there,
For those who will face the same problem, here is the solution.
First I had to determine the list of prefixes of the 100 segments (each segment contains a set of power traces). Then, I had to modify the add_trace() method a bit.
import scipy.io
import numpy as np
from os import walk
prefix_list = []
mypath = r’C:\Users…\Traces Template Attack\TmpTraces_RandPT_RandKey_20000_seg100_1_data\traces\’
f = []
for (dirpath, dirnames, filenames) in walk(mypath):
f.extend(filenames)
break
for filen in f:
# print(filen)
filen_split = filen.split("_")
if filen_split[1] == “keylist.npy”:
prefix_list.append(filen_split[0])
print("prefix list is of size: " + str(len(prefix_list)))
for i, prefix in enumerate(prefix_list):
n2m.add_trace(mypath + prefix)
Many thanks for your script - it got me started, i had to make some alterations and in your post not everything is formatted. Hopefully this will help others who are trying to use your script.
import scipy.io
import numpy as np
import sys
from os import walk
class native_to_matlab(object):
def __init__(self):
self.tracedata = None
self.textin = None
self.textout = None
self.key = None
def add_trace(self, prefix):
print(prefix)
_tracedata = np.load(prefix + '_0traces.npy', allow_pickle=True)
_textin = np.load(prefix + '_0textin.npy', allow_pickle=True)
_textout = np.load(prefix + '_0textout.npy', allow_pickle=True)
_key = np.load(prefix + '_0keylist.npy', allow_pickle=True)
if self.tracedata is None:
self.tracedata = _tracedata
self.textin = _textin
self.textout = _textout
self.key = _key
else:
self.tracedata = np.append(self.tracedata, _tracedata, axis=0)
self.textin = np.append(self.textin, _textin, axis=0)
self.textout = np.append(self.textout, _textout, axis=0)
self.key = np.append(self.key, _key, axis=0)
def write_matlab(self, fname):
file=(fname+'.mat')
scipy.io.savemat(file, {
"traces":self.tracedata,
"textin":self.textin,
"textout":self.textout,
"key":self.key
})
n2m = native_to_matlab()
prefix_list=[]
mypath = r'/home/alan/chipwhisperer/jupyter/alan_dev/test123_data/traces/'
f = []
for (dirpath, dirnames, filenames) in walk(mypath):
f.extend(filenames)
break
for filen in f:
#print(filen)
filen_split = filen.split("_")
#print (filen_split[0])
if filen_split[1] == "0keylist.npy":
prefix_list.append(filen_split[0])
print("prefix list is of size: " + str(len(prefix_list)))
for i, prefix in enumerate(prefix_list):
n2m.add_trace(mypath + prefix)
n2m.write_matlab("test_5.mat")