-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetup.py
66 lines (56 loc) · 1.91 KB
/
Setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import random
import serial
import scope
import TRS_TraceSet
# Si Gao 2017.12.5
# This is a Python script send/receive data through COM port
# It keeps sending until interrupted by keyboard
# Use the "single" triggering mod of the oscilloscope to capture one trace
# Figure out the trace you want and keeps the setup parameters for the actual acquisition
# Print Hex Data
def PrintHexData(data):
pdata = bytearray(data)
rwdstr = pdata.hex().upper()
# Insert spaces for between each 16-bits.
dstr = ''
i = 0
while i in range(len(rwdstr)):
dstr += rwdstr[i:i + 4] + ' '
i += 4
print(dstr)
return
def main():
# Serial port communication: look this up in 'device manager'
port='COM4' # Serial port
# Basic info about the target cipher
#As the key is hard-coded in the encryption device, no need to take care of the key
plain_len=16 # byte length of the plaintext
cipher_len=16 # byte length of the ciphertext
#Intiliazed random generator
random.seed()
#Open serial port
ser=serial.Serial(port)
i=0;
# Start sending
while(True):
# Generate random plaintext
plaintext = bytearray([random.getrandbits(8) for j in range (0,plain_len)])
# Send plaintext to the device
ser.write(plaintext)
# Read out ciphertext.
ciphertext = bytearray(ser.read(cipher_len))
# Print data
if i%100==0:
print("i="+str(i))
print("plain=")
PrintHexData(plaintext)
print("cipher=")
PrintHexData(ciphertext)
else:
pass
i=i+1;
# Close the serial port
ser.close()
return
if __name__ == '__main__':
main()