0% found this document useful (0 votes)
13 views1 page

Add CFI CFG

The document discusses cryptographic concepts like ciphertext traces, plaintext, known keys, and hamming distances. It loads numpy arrays from files containing this cryptographic data. It also defines a substitution box (sbox) which is a list of integers used for encryption/decryption. Feature selection and covariance matrix generation are mentioned for template attacks on this cryptographic data.

Uploaded by

asdfqewvgresvg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views1 page

Add CFI CFG

The document discusses cryptographic concepts like ciphertext traces, plaintext, known keys, and hamming distances. It loads numpy arrays from files containing this cryptographic data. It also defines a substitution box (sbox) which is a list of integers used for encryption/decryption. Feature selection and covariance matrix generation are mentioned for template attacks on this cryptographic data.

Uploaded by

asdfqewvgresvg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

import numpy as np

from scipy.stats import multivariate_normal


all_traces = np.load('./chipwhisperer/trac...')[:-1]
pt = np.load('./chipwhisperer/plai...')
knownkey = np.load('./chipwhisperer/key....')
hamming = tuple([bin(n).count('1') for n in range(256)])
sbox = (99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215,
171, 118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156,
164, 114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241,
113, 216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226,
235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179,
41, 227, 47, 132, 83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57,
74, 76, 88, 207, 208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127,
80, 60, 159, 168, 81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218,
33, 16, 255, 243, 210, 205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126,
61, 100, 93, 25, 115, 96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184,
20, 222, 94, 11, 219, 224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98,
145, 149, 228, 121, 231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, HammingClassGenerator
234, 101, 122, 174, 8, 186, 120, 37, 46, 28, 166, 180, 198, 232, 221, MatrixGenerator SubkeyGuessing
116, 31, 75, 189, 139, 138, 112, 62, 181, 102, 72, 3, 246, 14, 97, 53, FeatureSelector
cov """
87, 185, 134, 193, 29, 158, 225, 248, 152, 17, 105, 217, 142, 148, 155, KEY
Generate Hamming Distance Classes """ """
30, 135, 233, 206, 85, 40, 223, 140, 161, 137, 13, 191, 230, 66, 104, """
""" """ Compute pooled covriance matrix for template attacks Guess subkeys
65, 153, 45, 15, 176, 84, 187, 22) Feature Selection
Compute covariance between x and y outputSbox = [sbox[ptTrain[i][subkey] ^ knownkey[i][subkey]] for i in range """ """ try while for if
def cov(x, y):... """
""" (len(ptTrain))] meanMatrix = np.zeros((9, numFeatures)) key_rank = np.zeros(16)
def HammingClassGenerator(tracesTrain, ptTrain, subkey):... Means = np.zeros((9, len(tracesTrain[0])))
outputSboxHW = [hamming[s] for s in outputSbox] covMatrix = np.zeros((9, numFeatures, numFeatures)) P_k = np.zeros(256)
def FeatureSelector(tracesTrain, TracesHW, numFeatures, featureSpacing=5):...
TracesHW = [[] for _ in range(9)]
def MatrixGenerator(TracesHW, features, Means, numFeatures):...
def SubkeyGuessing(features, meanMatrix, pooled_covMatrix, subkey):...
intermediate = dict(zip(['TracesHW', 'features', 'Means',
'pooled_covMatrix', 'meanMatrix'], [0] * 5))
import constant as const
const.V_HAMMING = id(hamming)
const.V_SBOX = id(sbox)
const.V_TRACES = id(all_traces)
const.V_PLAIN = id(pt)
const.V_KEY = id(knownkey)
const.F_F0 = id(cov)
const.F_F1 = id(HammingClassGenerator)
const.F_F2 = id(FeatureSelector)
const.F_F3 = id(MatrixGenerator)
const.F_F4 = id(SubkeyGuessing)
const.ADDR_INTERMEDIATE = id(intermediate)
const.LEN_INTERMEDIATE = len(intermediate)
numFeatures = 25
number_of_training = 5000
number_of_tesing = 14
calls calls calls calls calls calls calls calls calls calls calls calls calls calls calls calls calls calls calls calls calls calls calls calls

len np.load np.load np.load tuple dict id id id id id id id id id id id if const.V_TRACES == id(all_traces) and const.V_PLAIN == id(pt return np.cov(x, y)[0][1] range range for i in range(len(tracesTrain)): np.zeros for i in range(9): np.zeros np.zeros for HW in range(9): np.zeros np.zeros for j in range(len(tracesTest)): raise return input

(not (const.V_TRACES == id(all_traces) and const.V_PLAIN == id(pt) and const.V_TRACES == id(all_traces) and const.V_PLAIN == id(pt
range(len(tracesTrain)) calls range(9) calls calls range(9) calls range(len(tracesTest))
const.V_KEY == id(knownkey))) ) and const.V_KEY == id(knownkey)
tracesTrain = all_traces[0:number_of_training]
print('Datasets have been r...') ptTrain = pt[0:number_of_training] HW = outputSboxHW[i]
bin.count range zip len TracesHW = [np.array(TracesHW[HW]) for HW in range(9)] range len SumDiff = np.zeros(len(tracesTrain[0])) Means[i] = np.average(TracesHW[i], 0) range range pooled_covMatrix = covMatrix.mean(axis=0) for i in range(numFeatures): range test_X = [tracesTest[j][features[i]] for i in range(len(features))] default call
assert True == False tracesTest = all_traces[9000:9000 + number_of_tesing] TracesHW[HW].append(tracesTrain[i])
ptTest = pt[9000:9000 + number_of_tesing]
calls True == False calls calls calls calls calls calls calls range(numFeatures) calls

print np.array range if id(intermediate) == const.ADDR_INTERMEDIATE and len(intermediate TracesHW.append len np.zeros for i in range(9): np.average covMatrix.mean if id(intermediate) == const.ADDR_INTERMEDIATE and len(intermediate range meanMatrix[HW][i] = Means[HW][features[i]] len range for kguess in range(0, 256):

id(intermediate) == const.ADDR_INTERMEDIATE and len(intermediate (not (id(intermediate) == const.ADDR_INTERMEDIATE and len(intermediate) == id(intermediate) == const.ADDR_INTERMEDIATE and len(intermediate (not (id(intermediate) == const.ADDR_INTERMEDIATE and len(intermediate) ==
range(9) calls calls range(0, 256)
) == const.LEN_INTERMEDIATE const.LEN_INTERMEDIATE)) ) == const.LEN_INTERMEDIATE const.LEN_INTERMEDIATE))
print('Top 10 guesses: ', P_k.argsort()[-10:...'t')
print( tarefs = np.argsort(P_k)[::-1] HW = hamming[sbox[ptTest[j][subkey] ^ kguess]]
SumDiff_old = SumDiff.copy()
print('The intermediate dic...') intermediate['pooled_covMatrix'] = pooled_covMatrix 'The intermediate dic...' key_rank[subkey] = list(tarefs).index(knownkey[j][subkey]) rv = multivariate_normal(meanMatrix[HW], pooled_covMatrix)
for subkey in range(16): intermediate['TracesHW'] = TracesHW len features = [] for j in range(i): range for j in range(numFeatures): len range
assert True == False intermediate['meanMatrix'] = meanMatrix ) print('Highest GE: ', key_rank[subkey], ...' p_kj = rv.pdf(test_X)
featureSpacing = featureSpacing
assert True == False P_k[kguess] += np.log(p_kj)
')
calls range(16) calls True == False calls calls range(i) calls True == False calls range(numFeatures) calls calls calls calls calls calls calls
x = TracesHW[HW][:, (features[i])]
range print('subkey[%d]' % subkey) print SumDiff.copy for i in range(numFeatures): range SumDiff += np.abs(Means[i] - Means[j]) print range y = TracesHW[HW][:, (features[j])] print print np.argsort list.index multivariate_normal rv.pdf np.log
covMatrix[HW, i, j] = cov(x, y)

calls calls range(numFeatures) calls calls


nextFeature = SumDiff.argmax()
features.append(nextFeature)
print if id(HammingClassGenerator) == const.F_F1: range if id(intermediate) == const.ADDR_INTERMEDIATE and len(intermediate np.abs cov P_k.argsort
featureMin = max(0, nextFeature - featureSpacing)
featureMax = min(nextFeature + featureSpacing, len(SumDiff))
id(intermediate) == const.ADDR_INTERMEDIATE and len(intermediate (not (id(intermediate) == const.ADDR_INTERMEDIATE and len(intermediate) ==
id(HammingClassGenerator) == const.F_F1 (id(HammingClassGenerator) != const.F_F1) calls calls calls calls calls
) == const.LEN_INTERMEDIATE const.LEN_INTERMEDIATE))

HammingClassGenerator(tracesTrain, ptTrain, subkey) print('The function has bee...') intermediate['features'] = features print('The intermediate dic...')
id features.append max min SumDiff.argmax for j in range(featureMin, featureMax):
TracesHW = intermediate['TracesHW'] assert True == False intermediate['Means'] = Means assert True == False

calls calls True == False calls range(featureMin, featureMax) calls True == False

HammingClassGenerator print len range SumDiff[j] = 0 print

if id(FeatureSelector) == const.F_F2:

id(FeatureSelector) == const.F_F2 (id(FeatureSelector) != const.F_F2) calls

FeatureSelector(tracesTrain, TracesHW, numFeatures) print('The function has bee...')


id
features, Means = intermediate['features'], intermediate['Means'] assert True == False

calls calls True == False

FeatureSelector print

if id(MatrixGenerator) == const.F_F3:

id(MatrixGenerator) == const.F_F3 (id(MatrixGenerator) != const.F_F3) calls


MatrixGenerator(TracesHW, features, Means, numFeatures)
print('The function has bee...')
pooled_covMatrix, meanMatrix = intermediate['pooled_covMatrix'], intermediate[ id
assert True == False
'meanMatrix']
calls calls True == False

MatrixGenerator print

if id(SubkeyGuessing) == const.F_F4:

(id(SubkeyGuessing) != const.F_F4) calls id(SubkeyGuessing) == const.F_F4

print('The function has bee...')


id SubkeyGuessing(features, meanMatrix, pooled_covMatrix, subkey)
assert True == False

True == False calls calls

print SubkeyGuessing

CFIonTA

You might also like