Skip to content

Commit

Permalink
api consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Feb 14, 2021
1 parent ca6427e commit 3e10b40
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 38 deletions.
2 changes: 1 addition & 1 deletion examples/half_handshake.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

def do_the_shake(alice, bob):
# exchange handshakes (encrypted with pubkey) over any insecure channel
bob_shake = bob.generate_secret(alice.pubkey)
bob_shake = bob.generate_handshake(alice.pubkey)

# read and verify handshakes
alice.receive_and_verify(bob_shake)
Expand Down
8 changes: 4 additions & 4 deletions examples/poor_pake.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@


def do_the_shake(alice, bob):
alice_shake = alice.send_handshake()
bob_shake = bob.send_handshake()
alice_shake = alice.generate_handshake()
bob_shake = bob.generate_handshake()

# exchange handshakes (hsubs) over any insecure channel
if not alice.receive_handshake(bob_shake):
if not alice.receive_and_verify(bob_shake):
raise KeyError
if not bob.receive_handshake(alice_shake):
if not bob.receive_and_verify(alice_shake):
raise KeyError

# a common key was derived from the password
Expand Down
4 changes: 2 additions & 2 deletions examples/simple_handshake.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def do_the_shake(alice, bob):
alice.load_public(bob.pubkey)

# exchange handshakes (encrypted with pubkey) over any insecure channel
alice_shake = alice.generate_secret()
bob_shake = bob.generate_secret()
alice_shake = alice.generate_handshake()
bob_shake = bob.generate_handshake()

# read and verify handshakes
bob.receive_and_verify(alice_shake)
Expand Down
4 changes: 2 additions & 2 deletions examples/simple_mitm.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def do_the_shake(alice, bob):
alice.load_public(bob.pubkey)

# exchange handshakes (encrypted with pubkey) over any insecure channel
alice_shake = alice.generate_secret()
bob_shake = bob.generate_secret()
alice_shake = alice.generate_handshake()
bob_shake = bob.generate_handshake()

# read and verify handshakes
bob.receive_and_verify(alice_shake)
Expand Down
4 changes: 2 additions & 2 deletions examples/static_handshake.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

def do_the_shake(alice, bob):
# exchange handshakes (encrypted with pubkey) over any insecure channel
alice_shake = alice.generate_secret()
bob_shake = bob.generate_secret()
alice_shake = alice.generate_handshake()
bob_shake = bob.generate_handshake()

# read and verify handshakes
bob.receive_and_verify(alice_shake)
Expand Down
4 changes: 2 additions & 2 deletions examples/static_mitm.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

def do_the_shake(alice, bob):
# exchange handshakes (encrypted with pubkey) over any insecure channel
alice_shake = alice.generate_secret()
bob_shake = bob.generate_secret()
alice_shake = alice.generate_handshake()
bob_shake = bob.generate_handshake()

# read and verify handshakes
bob.receive_and_verify(alice_shake)
Expand Down
4 changes: 2 additions & 2 deletions examples/tofu_handshake.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def do_the_shake(alice, bob):
print("Alice now trusts Bob")

# exchange handshakes (encrypted with pubkey) over any insecure channel
alice_shake = alice.generate_secret()
bob_shake = bob.generate_secret()
alice_shake = alice.generate_handshake()
bob_shake = bob.generate_handshake()

# read and verify handshakes
bob.receive_and_verify(alice_shake)
Expand Down
4 changes: 2 additions & 2 deletions examples/tofu_mitm.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def do_the_shake(alice, bob):
print("Alice now trusts Bob")

# exchange handshakes (encrypted with pubkey) over any insecure channel
alice_shake = alice.generate_secret()
bob_shake = bob.generate_secret()
alice_shake = alice.generate_handshake()
bob_shake = bob.generate_handshake()

# read and verify handshakes
bob.receive_and_verify(alice_shake)
Expand Down
24 changes: 12 additions & 12 deletions poorman_handshake/asymmetric/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def import_key(key_blob):
pubkey, _ = pgpy.PGPKey.from_blob(key_blob)
return pubkey

def generate_secret(self, pub=None):
def generate_handshake(self, pub=None):
pub = pub or self.target_key
# read pubkey from client
pubkey = self.import_key(pub)
Expand All @@ -59,34 +59,34 @@ def generate_secret(self, pub=None):
def load_public(self, pub):
self.target_key = pub

def receive_handshake(self, encrypted_message):
message_from_blob = pgpy.PGPMessage.from_blob(encrypted_message)
def receive_handshake(self, shake):
message_from_blob = pgpy.PGPMessage.from_blob(shake)
decrypted = self.private_key.decrypt(message_from_blob)
# XOR
self.secret = bytes(a ^ b for (a, b) in
zip(self.secret, decrypted.message))

def verify(self, encrypted_message, pub):
message = pgpy.PGPMessage.from_blob(encrypted_message)
def verify(self, shake, pub):
message = pgpy.PGPMessage.from_blob(shake)
pubkey = self.import_key(pub)
return pubkey.verify(message)

def receive_and_verify(self, encrypted_message, pub=None):
def receive_and_verify(self, shake, pub=None):
pub = pub or self.target_key
verified = self.verify(encrypted_message, pub)
verified = self.verify(shake, pub)
if verified:
self.receive_handshake(encrypted_message)
self.receive_handshake(shake)


class HalfHandShake(HandShake):

def generate_secret(self, pub=None):
enc = super().generate_secret(pub)
def generate_handshake(self, pub=None):
enc = super().generate_handshake(pub)
self.secret = bytes(self.secret)
return enc

def receive_handshake(self, encrypted_message):
message_from_blob = pgpy.PGPMessage.from_blob(encrypted_message)
def receive_handshake(self, shake):
message_from_blob = pgpy.PGPMessage.from_blob(shake)
decrypted = self.private_key.decrypt(message_from_blob)
# XOR
self.secret = bytes(decrypted.message)
Expand Down
18 changes: 13 additions & 5 deletions poorman_handshake/symmetric/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@ def __init__(self, password):
self.iv = None
self.salt = None

def send_handshake(self):
def generate_handshake(self):
self.iv = generate_iv()
return create_hsub(self.password, self.iv)

def receive_handshake(self, password):
if match_hsub(password, self.password):
self.salt = bytes(a ^ b for (a, b) in
zip(self.iv, iv_from_hsub(password)))
def receive_handshake(self, shake):
self.salt = bytes(a ^ b for (a, b) in
zip(self.iv, iv_from_hsub(shake)))

def receive_and_verify(self, shake):
if self.verify(shake):
self.receive_handshake(shake)
return True
return False

def verify(self, shake):
if match_hsub(shake, self.password):
return True
return False

Expand Down
9 changes: 5 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ securely exchange symmetric encryption keys over insecure channels
Basic usage below, check [examples](./examples) folder for more advanced usage

A session key can be exchanged if there is a pre shared password

```python
from poorman_handshake import PasswordHandShake
from secrets import compare_digest
Expand All @@ -15,8 +16,8 @@ password = "Super Secret Pass Phrase"
bob = PasswordHandShake(password)
alice = PasswordHandShake(password)

alice_shake = alice.send_handshake()
bob_shake = bob.send_handshake()
alice_shake = alice.generate_handshake()
bob_shake = bob.generate_handshake()

# exchange handshakes (hsubs) over any insecure channel
if not alice.receive_handshake(bob_shake):
Expand All @@ -42,8 +43,8 @@ bob.load_public(alice.pubkey)
alice.load_public(bob.pubkey)

# exchange handshakes (encrypted with pubkey) over any insecure channel
alice_shake = alice.generate_secret()
bob_shake = bob.generate_secret()
alice_shake = alice.generate_handshake()
bob_shake = bob.generate_handshake()

# read and verify handshakes
bob.receive_and_verify(alice_shake)
Expand Down

0 comments on commit 3e10b40

Please sign in to comment.