def test_equivalent_keys_shared_key_getter(self): alices = PrivateKey.generate() alicesP = alices.public_key bobs, bobsprime = self._gen_equivalent_raw_keys_couple() bobsP, bobsprimeP = bobs.public_key, bobsprime.public_key assert bobsP == bobsprimeP box_AB = Box(alices, bobsP) box_BA = Box(bobs, alicesP) box_BprimeA = Box(bobsprime, alicesP) assert box_AB.shared_key() == box_BA.shared_key() assert box_BprimeA.shared_key() == box_BA.shared_key()
def test_equivalent_keys_shared_key_getter(self): alices = PrivateKey.generate() alicesP = alices.public_key bobs, bobsprime = self._gen_equivalent_raw_keys_couple() bobsP, bobsprimeP = bobs.public_key, bobsprime.public_key assert bobsP == bobsprimeP box_AB = Box(alices, bobsP) box_BA = Box(bobs, alicesP) box_BprimeA = Box(bobsprime, alicesP) assert box_AB.shared_key() == box_BA.shared_key() assert box_BprimeA.shared_key() == box_BA.shared_key()
def test_shared_key_getter(self): """ RFC 7748 "Elliptic Curves for Security" gives a set of test parameters for the Diffie-Hellman key exchange on Curve25519: 6.1. [Diffie-Hellman on] Curve25519 [ . . . ] Alice's private key, a: 77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a Alice's public key, X25519(a, 9): 8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a Bob's private key, b: 5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb Bob's public key, X25519(b, 9): de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f Since libNaCl/libsodium shared key generation adds an HSalsa20 key derivation pass on the raw shared Diffie-Hellman key, which is not exposed by itself, we just check the shared key for equality. """ prv_A = (b"77076d0a7318a57d3c16c17251b26645" b"df4c2f87ebc0992ab177fba51db92c2a") pub_A = (b"8520f0098930a754748b7ddcb43ef75a" b"0dbf3a0d26381af4eba4a98eaa9b4e6a") prv_B = (b"5dab087e624a8a4b79e17f8b83800ee6" b"6f3bb1292618b6fd1c2f8b27ff88e0eb") pub_B = (b"de9edb7d7b7dc1b4d35b61c2ece43537" b"3f8343c85b78674dadfc7e146f882b4f") alices = PrivateKey(binascii.unhexlify(prv_A)) bobs = PrivateKey(binascii.unhexlify(prv_B)) alicesP = alices.public_key bobsP = bobs.public_key assert binascii.unhexlify(pub_A) == bytes(alicesP) assert binascii.unhexlify(pub_B) == bytes(bobsP) box_AB = Box(alices, bobsP) box_BA = Box(bobs, alicesP) assert box_AB.shared_key() == box_BA.shared_key()
def test_shared_key_getter(self): """ RFC 7748 "Elliptic Curves for Security" gives a set of test parameters for the Diffie-Hellman key exchange on Curve25519: 6.1. [Diffie-Hellman on] Curve25519 [ . . . ] Alice's private key, a: 77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a Alice's public key, X25519(a, 9): 8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a Bob's private key, b: 5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb Bob's public key, X25519(b, 9): de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f Since libNaCl/libsodium shared key generation adds an HSalsa20 key derivation pass on the raw shared Diffie-Hellman key, which is not exposed by itself, we just check the shared key for equality. """ prv_A = (b'77076d0a7318a57d3c16c17251b26645' b'df4c2f87ebc0992ab177fba51db92c2a') pub_A = (b'8520f0098930a754748b7ddcb43ef75a' b'0dbf3a0d26381af4eba4a98eaa9b4e6a') prv_B = (b'5dab087e624a8a4b79e17f8b83800ee6' b'6f3bb1292618b6fd1c2f8b27ff88e0eb') pub_B = (b'de9edb7d7b7dc1b4d35b61c2ece43537' b'3f8343c85b78674dadfc7e146f882b4f') alices = PrivateKey(binascii.unhexlify(prv_A)) bobs = PrivateKey(binascii.unhexlify(prv_B)) alicesP = alices.public_key bobsP = bobs.public_key assert binascii.unhexlify(pub_A) == bytes(alicesP) assert binascii.unhexlify(pub_B) == bytes(bobsP) box_AB = Box(alices, bobsP) box_BA = Box(bobs, alicesP) assert box_AB.shared_key() == box_BA.shared_key()
class KeyBox: def __init__(self, remote_nonce, remote_pubkey, local_nonce, local_seckey): self.remote_nonce = remote_nonce self.local_nonce = local_nonce self.box = Box(PrivateKey(local_seckey, HexEncoder), PublicKey(remote_pubkey, HexEncoder)) def native_box(self): return self.box def decrypt(self, encdata): data = self.box.decrypt(encdata, self.remote_nonce.get()) self.remote_nonce.increment() return data def encrypt(self, data): encdata = self.box.encrypt(data, self.local_nonce.get())[24::] self.local_nonce.increment() return encdata def shared_key(self): return binascii.hexlify(self.box.shared_key()).decode('ascii')
def listen(): global shared_secret global listener_stream global secretNotKnown global nonce global nonce2 global callInProgress p = pyaudio.PyAudio() listener_stream = p.open(format=p.get_format_from_width(WIDTH), channels=CHANNELS, rate=RATE, output=True, frames_per_buffer=Listen_CHUNK) PORT = 50007 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) s.bind(('', PORT)) s.listen(1) conn, addr = s.accept() # waits for a connection global waitingForCall if waitingForCall: if (conn): waitingForCall = False talker = threading.Thread(target=talk) talker.start() control.displayUserInputDuringCall() print('Connected by', addr) time.sleep(2) data = conn.recv(Listen_CHUNK) i = 1 print("first data received") print(len(data)) writer.start() global skbob pkalice = jsonpickle.decode(firebase.get('/fatman/pk', None)) pkalice = PublicKey(pkalice, encoder=HexEncoder) bob_box = Box(skbob, pkalice) data = bob_box.decrypt(data) shared_secret = bob_box.shared_key() talk_secret_box = nacl.secret.SecretBox(shared_secret) print("listen", shared_secret) secretNotKnown = False data = conn.recv(Listen_CHUNK) while data != '' and callInProgress: try: data = talk_secret_box.decrypt(data) data = oc.decode(data) jitter_buf.put(data) data = conn.recv(Listen_CHUNK) i = i + 1 except Exception: print("listen error warning: ", sys.exc_info()[0]) print("length of excepted data: ", len(data)) if len(data) == 0: callInProgress = False data = conn.recv(Listen_CHUNK - len(data)) time.sleep(.05) data = conn.recv(Listen_CHUNK) if (not callInProgress): break continue listener_stream.stop_stream() listener_stream.close() p.terminate() conn.close() callInProgress = False print("listen stopped")
def listen(): global shared_secret global listener_stream global secretNotKnown global nonce global nonce2 global callInProgress p = pyaudio.PyAudio() listener_stream = p.open(format=p.get_format_from_width(WIDTH), channels=CHANNELS, rate=RATE, output=True, frames_per_buffer=Listen_CHUNK) PORT = 50008 #23555#50007 changed to 50007 from 50008 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) s.bind(('', PORT)) s.listen(1) #caller = threading.Thread(target=call) #caller.start() conn, addr = s.accept() #here the thread waits for a connection global waitingForCall if waitingForCall: if (conn): waitingForCall = False talker = threading.Thread(target=talk) talker.start() control.displayUserInputDuringCall() print('Connected by', addr) time.sleep(2) data = conn.recv(Listen_CHUNK) # #1024 #might need this ? nonce = nacl.utils.random(nacl.secret.SecretBox.NONCE_SIZE) i = 1 print("first data received") print(len(data)) writer.start() #https://github.com/pyca/pynacl/blob/51acad0e34e125378d166d6bb9662408056702e0/tests/test_public.py #alice_box = Box(skalice, pkbob) global skbob pkalice = jsonpickle.decode(firebase.get('/littleboy/pk', None)) pkalice = PublicKey(pkalice, encoder=HexEncoder) bob_box = Box(skbob, pkalice) data = bob_box.decrypt(data) shared_secret = bob_box.shared_key() talk_secret_box = nacl.secret.SecretBox(shared_secret) print("listen:", shared_secret) secretNotKnown = False data = conn.recv(Listen_CHUNK) global callInProgress while data != '': try: data = talk_secret_box.decrypt(data) data = oc.decode(data) jitter_buf.put(data) data = conn.recv(Listen_CHUNK) # #1024 i = i + 1 except Exception: print("Error warning:", sys.exc_info()[0]) print("length of shit data", len(data)) if len(data) == 0: callInProgress = False data = conn.recv(Listen_CHUNK - len(data)) # turrible derter destreryer time.sleep(.05) data = conn.recv(Listen_CHUNK) # #1024s if (not callInProgress): break continue #break listener_stream.stop_stream() listener_stream.close() p.terminate() conn.close() print("listen stopped") callInProgress = False
def ecc_diffie_hellman(other_key): other_key = nacl.public.PublicKey(other_key) server_box = Box(priv, other_key) s_k = server_box.shared_key() return s_k
def DH(dh_pair, dh_pub): dh_pairM = dh_pair.to_curve25519_private_key() dh_pubM = VerifyKey(dh_pub, encoder=HexEncoder).to_curve25519_public_key() b = Box(dh_pairM, dh_pubM) return b.shared_key()
def dh(KP_L, PK_R): privk_L = KP_L.to_curve25519_private_key() pubk_R = PK_R.to_curve25519_public_key() box_LR = Box(privk_L, pubk_R) return box_LR.shared_key()