Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	.idea/.gitignore
#	.idea/WritingScript_SideChannelAnaylsis.iml
#	.idea/inspectionProfiles/Project_Default.xml
#	.idea/misc.xml
  • Loading branch information
yap231995 committed Jun 15, 2021
2 parents f7125eb + 386ab46 commit cbb4f3b
Show file tree
Hide file tree
Showing 27 changed files with 36 additions and 23 deletions.
1 change: 0 additions & 1 deletion .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/WritingScript_SideChannelAnaylsis.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 2 additions & 10 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions CPA.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import numpy as np
import os
import scipy.io as spio
import matplotlib.pyplot as plt

#import matplotlib.pyplot as plt

sbox = [0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
Expand Down Expand Up @@ -156,20 +155,19 @@ def Corr_with_correct_key(pt,traces,master_key, target_byte):


###Experiment2: Do CPA over number of sample
target_byte = 4
target_byte = 5
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')
spio.savemat("key_corr_over_trunc_traces_lst_target_byte"+ str(target_byte)+".mat", {'key_corr_over_trunc_traces_lst': key_corr_over_trunc_traces_lst}, do_compression=True, oned_as='row')
os.chdir('../..')


Expand All @@ -193,4 +191,4 @@ def Corr_with_correct_key(pt,traces,master_key, target_byte):
# if (master_key[target_byte] != hypothesis_key[target_byte]):
# print("Wrong " + str(target_byte))
# else:
# print("Correct " + str(target_byte))
# print("Correct " + str(target_byte))
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
32 changes: 28 additions & 4 deletions corr_key_to_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
## load key_corr
cwd = os.getcwd()
PATH_Results = os.path.join(cwd,"Results")
PATH = os.path.join(PATH_Results,"AES_my_own_2021-06-09_17_51_39")
PATH = os.path.join(PATH_Results,"AES_2021-06-09_17_51_39")

traces_fname = os.path.join(PATH, "corr_key.mat")
corr_key_lst_contents = spio.loadmat(traces_fname)
corr_key_lst_fname = os.path.join(PATH, "corr_key.mat")
corr_key_lst_contents = spio.loadmat(corr_key_lst_fname)
key_corr_lst = corr_key_lst_contents["key_corr_lst"]
max_index_lst = corr_key_lst_contents["max_index_lst"]
print("Hyporthesis key:")
Expand All @@ -34,4 +34,28 @@
# plt.savefig(image_fname)


##Experiment2
##Experiment2
target_byte = 4
key_corr_over_trunc_traces_lst_fname = os.path.join(PATH, "key_corr_over_trunc_traces_lst_target_byte"+str(target_byte)+".mat")
key_corr_over_trunc_traces_lst_contents = spio.loadmat(key_corr_over_trunc_traces_lst_fname)
key_corr_over_trunc_traces_lst = key_corr_over_trunc_traces_lst_contents["key_corr_over_trunc_traces_lst"]
Total_num_traces = 10000
x_axis = [i for i in range(100,Total_num_traces,100)]
#key_corr_over_trunc_traces_lst = np.array([list(os.urandom(256))]*99)


plt.figure()
for i in range(256):
if master_key[target_byte] == i:
plt.plot(x_axis, key_corr_over_trunc_traces_lst[:,i], "k")

else:
plt.plot(x_axis, key_corr_over_trunc_traces_lst[:,i], "r")
plt.title("Max absolute correlation for every key candidate for target_byte " + str(target_byte))
plt.xlabel("Number of Traces")
plt.ylabel("(Absolute) Correlation")

image_fname2 = os.path.join(PATH, 'abs max correlation over number of traces with target_byte '+ str(target_byte) + '.png')
plt.savefig(image_fname2)

plt.show()

0 comments on commit cbb4f3b

Please sign in to comment.