init
This commit is contained in:
commit
ec14efb417
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
k
|
||||||
|
mactext
|
||||||
|
|
||||||
|
*.pem
|
||||||
|
sigtext
|
||||||
28
part1/alice.py
Normal file
28
part1/alice.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import hmac
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
import secrets
|
||||||
|
|
||||||
|
# Generate and write shared key
|
||||||
|
|
||||||
|
shared_secret: bytes = secrets.token_bytes(16)
|
||||||
|
|
||||||
|
with open('k', 'wb') as file:
|
||||||
|
file.write(shared_secret)
|
||||||
|
|
||||||
|
# Take in message and compute hmac
|
||||||
|
m = input("Message: ").encode()
|
||||||
|
|
||||||
|
hmac_object: hmac.HMAC = hmac.new(shared_secret, m, hashlib.sha256)
|
||||||
|
m_hash = hmac_object.digest()
|
||||||
|
|
||||||
|
print(f'HMAC: {m_hash}')
|
||||||
|
|
||||||
|
# Write hmac digest and message
|
||||||
|
|
||||||
|
with open('mactext', 'wb') as file:
|
||||||
|
file.write(m + m_hash)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
29
part1/bob.py
Normal file
29
part1/bob.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import hmac
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
|
||||||
|
# Import shared secret
|
||||||
|
with open('k', 'rb') as file:
|
||||||
|
shared_key = file.read()
|
||||||
|
|
||||||
|
# Compute hmac
|
||||||
|
with open('mactext', 'rb') as file:
|
||||||
|
data = file.read()
|
||||||
|
|
||||||
|
m = data[:18]
|
||||||
|
m_hash = data[18:]
|
||||||
|
|
||||||
|
hmac_oject: hmac.HMAC = hmac.new(shared_key, m, hashlib.sha256)
|
||||||
|
derived_hmac = hmac_oject.digest()
|
||||||
|
print(f'Derived HMAC: {hmac_oject.digest()}')
|
||||||
|
|
||||||
|
if derived_hmac == m_hash:
|
||||||
|
print("HMAC matches message")
|
||||||
|
|
||||||
|
else:
|
||||||
|
print("HMAC does not match message")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
25
part2/alice.py
Normal file
25
part2/alice.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
from Crypto.PublicKey import RSA
|
||||||
|
from Crypto.Signature import pss
|
||||||
|
from Crypto.Hash import SHA256
|
||||||
|
|
||||||
|
key = RSA.generate(2048)
|
||||||
|
|
||||||
|
# Generate / write alice pub key
|
||||||
|
with open("alice_public.pem", "wb") as file:
|
||||||
|
file.write(key.publickey().export_key())
|
||||||
|
|
||||||
|
m = input("Message: ").encode()
|
||||||
|
|
||||||
|
h = SHA256.new(m)
|
||||||
|
|
||||||
|
signer = pss.new(key)
|
||||||
|
|
||||||
|
signature: bytes = signer.sign(h)
|
||||||
|
|
||||||
|
print(f'Signature: {signature}')
|
||||||
|
|
||||||
|
with open("sigtext", 'wb') as file:
|
||||||
|
file.write(m + signature)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
25
part2/bob.py
Normal file
25
part2/bob.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
from Crypto.PublicKey import RSA
|
||||||
|
from Crypto.Signature import pss
|
||||||
|
from Crypto.Hash import SHA256
|
||||||
|
|
||||||
|
with open("alice_public.pem", 'rb') as file:
|
||||||
|
alice_pub = RSA.import_key(file.read())
|
||||||
|
|
||||||
|
with open('sigtext', 'rb') as file:
|
||||||
|
data = file.read()
|
||||||
|
|
||||||
|
m = data[:18]
|
||||||
|
sig = data[18:]
|
||||||
|
|
||||||
|
h = SHA256.new(m)
|
||||||
|
|
||||||
|
verifier = pss.new(alice_pub)
|
||||||
|
|
||||||
|
try:
|
||||||
|
verifier.verify(h, sig)
|
||||||
|
print("Signature is valid")
|
||||||
|
|
||||||
|
except (ValueError, TypeError) as e:
|
||||||
|
print("Signature is invalid")
|
||||||
|
|
||||||
|
|
||||||
70
part3/speed_test.py
Normal file
70
part3/speed_test.py
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
|
import hmac
|
||||||
|
import hashlib
|
||||||
|
import secrets
|
||||||
|
|
||||||
|
m = input("Message: ").encode()
|
||||||
|
|
||||||
|
key = secrets.token_bytes(16)
|
||||||
|
|
||||||
|
sig_gen_times = []
|
||||||
|
sig_ver_times = []
|
||||||
|
|
||||||
|
# HMAC
|
||||||
|
|
||||||
|
for n in range(100):
|
||||||
|
|
||||||
|
start = time.time()
|
||||||
|
hmac_object = hmac.new(key, m, hashlib.sha256)
|
||||||
|
end = time.time()
|
||||||
|
|
||||||
|
sig_gen_times.append(end - start)
|
||||||
|
|
||||||
|
start = time.time()
|
||||||
|
assert hmac_object.digest() == hmac.new(key, m, hashlib.sha256).digest()
|
||||||
|
end = time.time()
|
||||||
|
|
||||||
|
sig_ver_times.append(end - start)
|
||||||
|
|
||||||
|
avg_gen_time = (sum(sig_gen_times)/len(sig_gen_times)) * 1000
|
||||||
|
avg_ver_time = (sum(sig_ver_times)/len(sig_ver_times)) * 1000
|
||||||
|
|
||||||
|
print(f"Average HMAC Generation Time: {avg_gen_time} ms")
|
||||||
|
print(f"Average HMAC Verification Time: {avg_ver_time} ms")
|
||||||
|
|
||||||
|
# RSA
|
||||||
|
|
||||||
|
from Crypto.PublicKey import RSA
|
||||||
|
from Crypto.Signature import pss
|
||||||
|
from Crypto.Hash import SHA256
|
||||||
|
|
||||||
|
sig_gen_times = []
|
||||||
|
sig_ver_times = []
|
||||||
|
|
||||||
|
key = RSA.generate(2048)
|
||||||
|
|
||||||
|
for n in range(100):
|
||||||
|
|
||||||
|
start = time.time()
|
||||||
|
h = SHA256.new(m)
|
||||||
|
signer = pss.new(key)
|
||||||
|
signature = signer.sign(h)
|
||||||
|
end = time.time()
|
||||||
|
|
||||||
|
sig_gen_times.append(end - start)
|
||||||
|
public_key = key.publickey()
|
||||||
|
|
||||||
|
start = time.time()
|
||||||
|
h = SHA256.new(m)
|
||||||
|
verifier = pss.new(public_key)
|
||||||
|
verifier.verify(h, signature)
|
||||||
|
end = time.time()
|
||||||
|
|
||||||
|
sig_ver_times.append(end - start)
|
||||||
|
|
||||||
|
avg_gen_time = (sum(sig_gen_times)/len(sig_gen_times)) * 1000
|
||||||
|
avg_ver_time = (sum(sig_ver_times)/len(sig_ver_times)) * 1000
|
||||||
|
|
||||||
|
print(f"Average RSA Signature Generation Time: {avg_gen_time} ms")
|
||||||
|
print(f"Average RSA Signature Verification Time: {avg_ver_time} ms")
|
||||||
0
requirements.txt
Normal file
0
requirements.txt
Normal file
Loading…
x
Reference in New Issue
Block a user