# Load measured traces into a matrix: trace-number x trace-length traces = input_data.get_traces() # Load ciphertexts into a matrix: trace-number x 16 bytes ciphertext ciphertexts = input_data.get_ciphertexts() # Load plaintexts into a matrix: trace-number x 16 bytes plaintext plaintexts = input_data.get_plaintexts() ############################### PERFORM DPA #################################### # last_round_key = dpa.perform_dpa(ciphertexts, traces) #last_round_key = dpa.perform_dpa(ciphertexts, traces) timeList = [] compressRate = [] rate = 5 last_round_key, COR = dpa.perform_dpa(plaintexts, traces, rate) ############################ TEST RESULTS ################################### if test_key.test_key(last_round_key, plaintexts[0], ciphertexts[0]): print "Congratulations! Your key is right." # compressRate.append(rate) # rate += 50 # print t2 -t1 # timeList.append(t2 - t1) # print COR # plt.plot(COR, label = "rate " + str(rate)) # plt.ylabel('COR') # plt.xlabel('KEY') # plt.show()
plaintexts = input_data.get_plaintexts() ############################### PERFORM DPA #################################### # last_round_key = dpa.perform_dpa(ciphertexts, traces) #last_round_key = dpa.perform_dpa(ciphertexts, traces) timeList = [] compressRate = [] rate = 40 BYTE16=16 # while(1): if __name__ == '__main__': t1 = time.clock() last_round_key, COR , DiffTrace , TRACES, SAMPLE, dpa_init_done = dpa.perform_dpa(traces, rate) if dpa_init_done == True : pool = multiprocessing.Pool(processes=4) iterable= range(16) plain=plaintexts func= partial(dpa.getKeyForByte,plain,last_round_key,DiffTrace,TRACES,SAMPLE) results =[ ] r = pool.map(func, iterable) # Wait on the result pool.close(); pool.terminate(); last_round_key=np.array(r,np.uint8) t2 = time.clock() print (t2-t1) ############################# TEST RESULTS ################################### if test_key.test_key(last_round_key, plaintexts[0], ciphertexts[0]):
plaintext = data['plaintext'][()].astype(int).T ciphertext = data['ciphertext'][()].astype(int).T traces = data['traces'][()].T else: data = scipy.io.loadmat(filename, appendmat=False) plaintext = data['plaintext'] ciphertext = data['ciphertext'] traces = data['traces'] print 'Starting DPA...' start = time.clock() traces = preprocessing(traces) #dpaResult = perform_dpa(plaintext[1000:1250], traces[1000:1250], showPlot=True, fullPlot=True) dpaResult = perform_dpa(plaintext, traces, showPlot=True, fullPlot=False) end = time.clock() duration = end - start print "\tTime:\t%0.2lfs" % (duration) # masterKeyVec = dpaResult[0] # maxCorrsVec = dpaResult[1] # # print "\nDPA Result:" # print "\tTraces:\tn = %d (%d samples per trace)" % (traces.shape[0], traces.shape[1]) # print "\tTime:\t%0.2lfs (%0.2lfs per key byte)" % (duration, duration/16) # # masterKey = hexVector2number(masterKeyVec) # print "\tKey:\t0x%0.32X" % masterKey # #
# Load measured traces into a matrix: trace-number x trace-length traces = input_data.get_traces() # Load ciphertexts into a matrix: trace-number x 16 bytes ciphertext ciphertexts = input_data.get_ciphertexts() # Load plaintexts into a matrix: trace-number x 16 bytes plaintext plaintexts = input_data.get_plaintexts() ############################### PERFORM DPA #################################### # last_round_key = dpa.perform_dpa(ciphertexts, traces) #last_round_key = dpa.perform_dpa(ciphertexts, traces) timeList = [] compressRate = [] rate = 5 last_round_key, COR = dpa.perform_dpa(plaintexts, traces, rate) ############################ TEST RESULTS ################################### if test_key.test_key(last_round_key, plaintexts[0], ciphertexts[0]): print "Congratulations! Your key is right." # compressRate.append(rate) # rate += 50 # print t2 -t1 # timeList.append(t2 - t1) # print COR # plt.plot(COR, label = "rate " + str(rate)) # plt.ylabel('COR') # plt.xlabel('KEY') # plt.show() else:
timing = np.zeros(numRuns) results = np.zeros(numRuns) for run in range(numRuns): # mask = np.random.permutation(mask) # curTraces = traces[mask,:] # curPlaintext = plaintext[mask,:] # curCiphertext = ciphertext[mask,:] curTraces = traces[stepSize*run:,:] curPlaintext = plaintext[stepSize*run:,:] curCiphertext = ciphertext[stepSize*run:,:] start = time.clock() dpaResult = perform_dpa(curPlaintext, curTraces, showPlot=False, fullPlot=False, silent=True) end = time.clock() masterKeyVec = dpaResult[0] maxCorrs[run, :] = dpaResult[1] duration = end - start print "\nDPA %d. run:" % (run + 1) print "\tTraces:\tn = %d (%d samples per trace)" % (curTraces.shape[0], curTraces.shape[1]) print "\tTime:\t%0.2lfs (%0.2lfs per key byte)" % (duration, duration/16) masterKey = hexVector2number(masterKeyVec) print "\tKey:\t0x%0.32X" % masterKey ## Test Key from DPA:
# Load plaintexts into a matrix: trace-number x 16 bytes plaintext plaintexts = input_data.get_plaintexts() ############################### PERFORM DPA #################################### # last_round_key = dpa.perform_dpa(ciphertexts, traces) #last_round_key = dpa.perform_dpa(ciphertexts, traces) timeList = [] compressRate = [] rate = 40 BYTE16 = 16 # while(1): if __name__ == '__main__': t1 = time.clock() last_round_key, COR, DiffTrace, TRACES, SAMPLE, dpa_init_done = dpa.perform_dpa( traces, rate) if dpa_init_done == True: pool = multiprocessing.Pool(processes=4) iterable = range(16) plain = plaintexts func = partial(dpa.getKeyForByte, plain, last_round_key, DiffTrace, TRACES, SAMPLE) results = [] r = pool.map(func, iterable) # Wait on the result pool.close() pool.terminate() last_round_key = np.array(r, np.uint8) t2 = time.clock() print(t2 - t1) ############################# TEST RESULTS ###################################