[10pts] Part 0: Hash Digest

The program sha256sum shows the (SHA-256) hash of the input file. See the sample run below:
~$ sha256sum pcap.tar
f616da6e0bf7fee0c1e3487ac9ae6729e6272f6e49c4918ec3c9bfdb6cd82335  pcap.tar

Your Task

Write a python program sha256sum.py that works essentially the same as the program sha256sum.
~$ ./sha256sum.py pcap.tar
f616da6e0bf7fee0c1e3487ac9ae6729e6272f6e49c4918ec3c9bfdb6cd82335  pcap.tar
Download: pcap.tar

Deliverables

[10pts] Part 1: HMAC

Write code lab09.py that implements the following function:

def computeHMAC(key, data):
Sample run with pic_original.bmp.

>>> import lab09

>>> key = b"0123456789abcdef"
>>> data = open("pic_original.bmp", "rb").read()
>>> digest = lab09.computeHMAC(key, data)
>>> digest
b'c2\xba$\x93@\xa4\x9d\xe2\x07\x065`\xb0\xe5$\xb6F\xd5\xact>\x02\xf88r\xc1\x81\x04\xf3\xa45'

>>> key = b"my really secret key"
>>> digest = lab09.computeHMAC(key, data)
>>> digest
b'G]\x90\xb5\xd2:@s\xc3y\x1b\xed\x8a\x9d+$\xbe\r\x9b\x14g\xed \x06\x0c\x813%d\x889F'
Note: You must pass the test case in the submit server.

[20pts] Part 2: Diffie-Hellman Key Exchange: Functions keyA() and keyB()

In dh.py, first complete functions

Sample Run 1

The following sample run uses the example we saw during the lecture:

>>> from dh import *
>>> g = 4
>>> p = 23
>>> a = 4
>>> A = 3
>>> b = 6
>>> B = 2
>>> Ka = keyA(g, p, A, a, B)
>>> Ka
16
>>> Kb = keyB(g, p, B, b, A)
>>> Kb
16

Sample Run 2

The following sample run deals with more realistic numbers to be used in practice. The sample run used the following data file:

>>> from dh import *
>>> from data import *
>>> p = prime()
>>> g = generator()
>>> keyA(g, p, A, a, B)
77617479304829027680043603673290826241660300475792061468464737406946474152345364558688703575749
71058259918221966259909932034649763285341324192055758628838861767443226376855250551208377751850
70708632041988593174133177579318754489528440542354969144661164909587476418402515315385666566059
84911552178797345945075123788731687229571223698916603050703680810725394447355368193761189664720
64313211646836859435270808735607826853560045988374009351982055177533679531638777050792575134962
47020061989259666215605544230669916660513891900391147492913141631733724916146418739697648529141
8822084063732190956764018175484941169412374489
>>> keyB(g, p, B, b, A)
77617479304829027680043603673290826241660300475792061468464737406946474152345364558688703575749
71058259918221966259909932034649763285341324192055758628838861767443226376855250551208377751850
70708632041988593174133177579318754489528440542354969144661164909587476418402515315385666566059
84911552178797345945075123788731687229571223698916603050703680810725394447355368193761189664720
64313211646836859435270808735607826853560045988374009351982055177533679531638777050792575134962
47020061989259666215605544230669916660513891900391147492913141631733724916146418739697648529141
8822084063732190956764018175484941169412374489
Note: You must pass the test case in the submit server.

[20pts] Part 3: Diffie-Hellman Key Exchange: Functions sendA() and sendB()

Now, complete functions Note that the functions choose a random exponent, the output will be different every time.

Sample Run (read the sample run carefully)


>>> from dh import *
>>> g = 4
>>> p = 23
>>> (A, a) = sendA(g, p)
>>> (A, a)
(18, 3)
>>> sendA(g, p)
(12, 5)
>>> (B, b) = sendB(g, p)
>>> (B, b)
(9, 8)
>>> sendB(g, p)
(4, 1)
>>> (A, a) = sendA(g, p); (B, b) = sendB(g, p); Ka = keyA(g, p, A, a, B); Kb = keyB(g, p, B, b, A); Ka == Kb; Ka
True
2
>>> (A, a) = sendA(g, p); (B, b) = sendB(g, p); Ka = keyA(g, p, A, a, B); Kb = keyB(g, p, B, b, A); Ka == Kb; Ka
True
9
>>> g = generator()
>>> p = prime()
>>> (A, a) = sendA(g, p); (B, b) = sendB(g, p); Ka = keyA(g, p, A, a, B); Kb = keyB(g, p, B, b, A); Ka == Kb; Ka
True
71514005803959937963804965818009202233119663395088512840880667787859520850597444874790918861367314110096283085686
84705343669200464366850167869925532856823407002323521719373485458080231515327127148476090153800504389157289108388
11899562489284976640519822360076104363378145520349812560736214952026033937582969107222686459667589498093497394619
07335486674015867341350056829756733400956913315327297502227259512094022563851107889712854318295936646849424090069
01339164045081421289055549020126084109520665402968230487934782330317329248895130220651104707342450857615909812500
>>> (A, a) = sendA(g, p); (B, b) = sendB(g, p); Ka2 = keyA(g, p, A, a, B); Kb2 = keyB(g, p, B, b, A); Ka2 == Kb2; Ka == Ka2
True
False
>>> (A, a) = sendA(g, p); (B, b) = sendB(g, p); Ka2 = keyA(g, p, A, a, B); Kb2 = keyB(g, p, B, b, A); Ka2 == Kb2; Ka == Ka2
True
False
Note: You must pass the test case in the submit server.

[30pts] Part 4: Encrypted Netcat

In this part, we are going to write an encrypted netcat program.

Your Task

Sample Run

Red texts are the user input.
~$ python3 alice.py
Hello Bob
My name is Alice
--> Hi Alice
--> I am Bob
quit

  
~$ python3 bob.py
b'Hello'
--> Hello Bob
--> My name is Alice
Hi Alice
I am Bob
--> quit

[10pts] Part 5: Lab Report and Submission

Write a lab report by using the provided template. The writing quality of the lab report matters.

Deliverables

~/bin/submit -c=IT430 -p=lab09 lab09_report.docx *.py