Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor committed Jun 11, 2021
2 parents 8af1a57 + 0dd228a commit 586fbbd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
54 changes: 29 additions & 25 deletions CPA.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def Corr_with_correct_key(pt,traces,master_key, target_byte):
traces_fname = os.path.join(PATH_traces, "traces_0.mat")
traces_contents = spio.loadmat(traces_fname)
traces = traces_contents["traces"]

traces = traces[:, 5000:17500] #Truncate the traces according to the trigger:


Expand All @@ -141,32 +142,35 @@ def Corr_with_correct_key(pt,traces,master_key, target_byte):
pt = pt[:num_traces]
print("Finished loading Plaintext")



master_key = np.array([0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c])
max_index_lst = []
key_corr_lst = []
for target_byte in range(16):
key_corr, max_index, max_val = CPA_traces_per_target_byte(pt, traces, target_byte)
key_corr_lst.append(key_corr)
max_index_lst.append(max_index)
# plt.plot(key_corr)
# plt.plot(master_key[target_byte],key_corr[master_key[target_byte]],'k*')
# plt.title("Max absolute correlation for every key candidate")
# plt.xlabel("Key Candidates")
# plt.ylabel("(Absolute) Correlation")
# image_fname = os.path.join(PATH_image, 'Max absolute correlation for every key candidate with target_byte '+ str(target_byte) + '.png')
# plt.savefig(image_fname)
#


spio.savemat("corr_key.mat", {'key_corr_lst': key_corr_lst, 'max_index_lst': max_index_lst}, do_compression=True, oned_as='row')
# print(corr_matrix)





####Experiment1: Do CPA for each byte find the max abs correlation for every key candidate using all the traces
# max_index_lst = []
# key_corr_lst = []
# for target_byte in range(16):
# key_corr, max_index, max_val = CPA_traces_per_target_byte(pt, traces, target_byte)
# key_corr_lst.append(key_corr)
# max_index_lst.append(max_index)
# spio.savemat("corr_key.mat", {'key_corr_lst': key_corr_lst, 'max_index_lst': max_index_lst}, do_compression=True, oned_as='row')
# load it and use corr_key_to_image.py in Experiment 1 for all graph and obtain the graph.



###Experiment2: Do CPA over number of sample
target_byte = 4
x_axis_value = []
key_corr_over_trunc_traces_lst = []
for no_trace in range(100,traces.shape[0], 100):
x_axis_value.append(no_trace)
trunc_trace = traces[:no_trace,:]
#key_corr = list(os.urandom(256))
key_corr, _,_ = CPA_traces_per_target_byte(pt, trunc_trace, target_byte) #key_corr = max correlation of each candidate key->key_corr.shape = (256,)
key_corr_over_trunc_traces_lst.append(key_corr)
key_corr_over_trunc_traces_lst = np.array(key_corr_over_trunc_traces_lst)
PATH_Results = os.path.join(cwd,"Results")
PATH_Results_specific = os.path.join(PATH_Results,"AES_my_own_2021-06-09_17_51_39")
os.chdir(PATH_Results_specific)
spio.savemat("key_corr_over_trunc_traces_lst.mat", {'key_corr_over_trunc_traces_lst': key_corr_over_trunc_traces_lst}, do_compression=True, oned_as='row')
os.chdir('../..')



Expand Down
25 changes: 15 additions & 10 deletions corr_key_to_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
master_key = np.array([0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c])
print("Correct Key:")
print(master_key)
for target_byte in range(16):
key_corr = key_corr_lst[target_byte]
plt.figure()
plt.plot(key_corr)
plt.plot(master_key[target_byte],key_corr[master_key[target_byte]],'k*')
plt.title("Max absolute correlation for every key candidate")
plt.xlabel("Key Candidates")
plt.ylabel("(Absolute) Correlation")
image_fname = os.path.join(PATH, 'Max absolute correlation for every key candidate with target_byte '+ str(target_byte) + '.png')
plt.savefig(image_fname)

## Experiment 1
# for target_byte in range(16):
# key_corr = key_corr_lst[target_byte]
# plt.figure()
# plt.plot(key_corr)
# plt.plot(master_key[target_byte],key_corr[master_key[target_byte]],'k*')
# plt.title("Max absolute correlation for every key candidate")
# plt.xlabel("Key Candidates")
# plt.ylabel("(Absolute) Correlation")
# image_fname = os.path.join(PATH, 'Max absolute correlation for every key candidate with target_byte '+ str(target_byte) + '.png')
# plt.savefig(image_fname)


##Experiment2

0 comments on commit 586fbbd

Please sign in to comment.