def exchange_test(): private = PrivateKey().load( bytes.fromhex( "309368A4418E889426F4655235E3605DA3D9EF9F942727C76D32EBA3A5579E41") ) public = PublicKey( bytes.fromhex( "436D986682B88E668F383B8D7155B8CB30FFC739EDA65E198D471087C596B17B") ) shared = private.do_exchange(public) print(shared.hex())
def test_init_typecheck(self): ''' Test type checking on init. ''' mv = memoryview(self.alice.private) with self.assertRaises(TypeError, msg='PrivateKey failed init type check.'): alice = PrivateKey(mv) ba = bytearray(self.alice.private) with self.assertRaises(TypeError, msg='PrivateKey failed init type check.'): alice = PrivateKey(ba) other = int.from_bytes(TVa, byteorder='big') with self.assertRaises(TypeError, msg='PrivateKey failed init type check.'): alice = PrivateKey(other)
def test_load_typecheck(self): ''' Test type checking on load. Added in case load starts to use its own type checking. ''' mv = memoryview(self.alice.private) with self.assertRaises(TypeError, msg='PrivateKey failed init type check.'): alice = PrivateKey.load(mv) ba = bytearray(self.alice.private) with self.assertRaises(TypeError, msg='PrivateKey failed init type check.'): alice = PrivateKey.load(ba) other = int.from_bytes(TVa, byteorder='big') with self.assertRaises(TypeError, msg='PrivateKey failed init type check.'): alice = PrivateKey.load(other)
def test_init_lencheck(self): ''' Test length checking on init. ''' less = bytes(31) with self.assertRaises(ValueError, msg='PrivateKey failed init len check.'): test = PrivateKey(less) more = bytes(33) with self.assertRaises(ValueError, msg='PrivateKey failed init len check.'): test = PrivateKey(more) edge = bytes() with self.assertRaises(ValueError, msg='PrivateKey failed init len check.'): test = PrivateKey(edge)
def test_load_lencheck(self): ''' Test length checking on init. Added in case load starts to use its own length checking. ''' less = bytes(31) with self.assertRaises(ValueError, msg='PrivateKey failed init len check.'): test = PrivateKey.load(less) more = bytes(33) with self.assertRaises(ValueError, msg='PrivateKey failed init len check.'): test = PrivateKey.load(more) edge = bytes() with self.assertRaises(ValueError, msg='PrivateKey failed init len check.'): test = PrivateKey.load(edge)
def test_randoms(self): ''' Test 100 random exchanges against each other. ''' for __ in range(100): alice = PrivateKey() bob = PrivateKey() a_to_b = alice.do_exchange(bob.get_public()) b_to_a = bob.do_exchange(alice.get_public()) self.assertEqual(a_to_b, b_to_a, 'Random key exchange mismatch.')
def test_load(self): alice = self.alice.private bob = self.bob.private reloada = PrivateKey.load(alice) reloadb = PrivateKey.load(bob) self.assertEqual( self.alice.private, reloada.private, 'Alice private key loading failed equality check.' ) self.assertEqual( self.bob.private, reloadb.private, 'Bob private key loading failed equality check.' )
class TestExchange(unittest.TestCase): def setUp(self): self.alice = PrivateKey(TVa) self.bob = PrivateKey(TVb) def test_alicebob(self): exchange = self.alice.do_exchange(self.bob.get_public()) self.assertEqual(exchange, TVab, 'Alice->Bob test vector mismatch.') def test_bobalice(self): exchange = self.bob.do_exchange(self.alice.get_public()) self.assertEqual(exchange, TVab, 'Bob->Alice test vector mismatch.') def test_randoms(self): ''' Test 100 random exchanges against each other. ''' for __ in range(100): alice = PrivateKey() bob = PrivateKey() a_to_b = alice.do_exchange(bob.get_public()) b_to_a = bob.do_exchange(alice.get_public()) self.assertEqual(a_to_b, b_to_a, 'Random key exchange mismatch.')
def _from_serialized(cls, condensed): try: ghid = Ghid.from_bytes(condensed['ghid']) keys = { 'signature': serialization.load_der_private_key(data=condensed['signature'], password=None, backend=CRYPTO_BACKEND), 'encryption': serialization.load_der_private_key( data=condensed['encryption'], password=None, backend=CRYPTO_BACKEND), 'exchange': ECDHPrivate.load(condensed['exchange']) } except (TypeError, KeyError) as e: raise TypeError( 'serialization must be compatible with _serialize.') from e return cls(keys=keys, ghid=ghid)
def _from_serialized(cls, condensed): try: ghid = Ghid.from_bytes(condensed['ghid']) keys = { 'signature': serialization.load_der_private_key( data = condensed['signature'], password = None, backend = CRYPTO_BACKEND ), 'encryption': serialization.load_der_private_key( data = condensed['encryption'], password = None, backend = CRYPTO_BACKEND ), 'exchange': ECDHPrivate.load(condensed['exchange']) } except (TypeError, KeyError) as e: raise TypeError( 'serialization must be compatible with _serialize.' ) from e return cls(keys=keys, ghid=ghid)
def setUp(self): self.alice = PrivateKey(TVa) self.bob = PrivateKey(TVb)
def setUp(self): ''' Test __init__ and create Alice and Bob from TVa, TVb above. ''' self.alice = PrivateKey(TVa) self.bob = PrivateKey(TVb)
class TestPrivate(unittest.TestCase): def setUp(self): ''' Test __init__ and create Alice and Bob from TVa, TVb above. ''' self.alice = PrivateKey(TVa) self.bob = PrivateKey(TVb) def test_private_get(self): alice = self.alice.private bob = self.bob.private def test_private_set(self): ''' Ensure PrivateKey.private is read-only. ''' with self.assertRaises(AttributeError, msg='alice.private failed set test'): self.alice.private = bytes(32) with self.assertRaises(AttributeError, msg='bob.private failed set test'): self.bob.private = bytes(32) def test_private_del(self): ''' Ensure PrivateKey.private is not deletable. ''' with self.assertRaises(AttributeError, msg='alice.private failed del test'): del self.alice.private with self.assertRaises(AttributeError, msg='bob.private failed del test'): del self.bob.private def test_successful_clamp(self): ''' Ensure clamping is performed correctly. ''' alice = self.alice.private bob = self.bob.private self.assertEqual(alice, TVa_clamped, 'Alice private clamp mismatch') self.assertEqual(bob, TVb_clamped, 'Bob private clamp mismatch') def test_load(self): alice = self.alice.private bob = self.bob.private reloada = PrivateKey.load(alice) reloadb = PrivateKey.load(bob) self.assertEqual( self.alice.private, reloada.private, 'Alice private key loading failed equality check.' ) self.assertEqual( self.bob.private, reloadb.private, 'Bob private key loading failed equality check.' ) def test_load_typecheck(self): ''' Test type checking on load. Added in case load starts to use its own type checking. ''' mv = memoryview(self.alice.private) with self.assertRaises(TypeError, msg='PrivateKey failed init type check.'): alice = PrivateKey.load(mv) ba = bytearray(self.alice.private) with self.assertRaises(TypeError, msg='PrivateKey failed init type check.'): alice = PrivateKey.load(ba) other = int.from_bytes(TVa, byteorder='big') with self.assertRaises(TypeError, msg='PrivateKey failed init type check.'): alice = PrivateKey.load(other) def test_load_lencheck(self): ''' Test length checking on init. Added in case load starts to use its own length checking. ''' less = bytes(31) with self.assertRaises(ValueError, msg='PrivateKey failed init len check.'): test = PrivateKey.load(less) more = bytes(33) with self.assertRaises(ValueError, msg='PrivateKey failed init len check.'): test = PrivateKey.load(more) edge = bytes() with self.assertRaises(ValueError, msg='PrivateKey failed init len check.'): test = PrivateKey.load(edge) def test_init_typecheck(self): ''' Test type checking on init. ''' mv = memoryview(self.alice.private) with self.assertRaises(TypeError, msg='PrivateKey failed init type check.'): alice = PrivateKey(mv) ba = bytearray(self.alice.private) with self.assertRaises(TypeError, msg='PrivateKey failed init type check.'): alice = PrivateKey(ba) other = int.from_bytes(TVa, byteorder='big') with self.assertRaises(TypeError, msg='PrivateKey failed init type check.'): alice = PrivateKey(other) def test_init_lencheck(self): ''' Test length checking on init. ''' less = bytes(31) with self.assertRaises(ValueError, msg='PrivateKey failed init len check.'): test = PrivateKey(less) more = bytes(33) with self.assertRaises(ValueError, msg='PrivateKey failed init len check.'): test = PrivateKey(more) edge = bytes() with self.assertRaises(ValueError, msg='PrivateKey failed init len check.'): test = PrivateKey(edge) def test_get_public(self): ''' Check functionality and validity of test_get_public. ''' publica = self.alice.get_public() publicb = self.bob.get_public() self.assertEqual( publica.public, TVa_public, 'Alice private to public key conversion failed equality check.' ) self.assertEqual( publicb.public, TVb_public, 'Bob private to public key conversion failed equality check.' )
import socket from donna25519 import PrivateKey from donna25519 import PublicKey import base64 import pickle variable_1 = PrivateKey() # se muestra la llave privada generada print(variable_1) variable_2 = PrivateKey().get_public().public # se muestra la llave publica de 32 bytes correspondiente a la PrivateKey print(variable_2) pk = pickle.dumps(variable_2) HOST = '127.0.0.1' # Standard loopback interface address (localhost) PORT = 65432 # Port to listen on (non-privileged ports are > 1023) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.bind((HOST, PORT)) s.listen() conn, addr = s.accept() with conn: print('Connected by', addr) while True: data = conn.recv(1024) if not data: break # se envía por medio del socket en bloque la llave publica de 32 bytes conn.sendall(pk)
p256_time = ecdh('secP256r1') print("P256 time:", p256_time, "s") p384_time = ecdh('secP384r1') print("P384 time:", p384_time, "s") p521_time = ecdh('secP521r1') print("P521 time:", p521_time, "s") print('=' * 30) print() # Curve 25519 ECDH print('=' * 30) print("Curve 25519 ECDH\n") start = time.time() sk_alice = PrivateKey(os.urandom(32)) pk_alice = sk_alice.get_public() sk_bob = PrivateKey(os.urandom(32)) pk_bob = sk_bob.get_public() alice_ss = sk_alice.do_exchange(pk_bob) bob_ss = sk_bob.do_exchange(pk_alice) elapsed_time = time.time() - start print("Alice Shared Secret:", alice_ss) print("Bob Shared Secret:", bob_ss) print("Shared Secrets Equal:", bob_ss == alice_ss) print("Time:", elapsed_time) print('=' * 30) print() # ECDSA w/ NIST P512 print('=' * 30)
def generation_exchange_test(): private = PrivateKey() public = PublicKey(public=os.urandom(32)) shared = private.do_exchange(public) print(shared.hex())
def curve25519GenerateKeyAgreement(privateKey, publicKey): exchangepartner = PrivateKey(privateKey) pubKey = PublicKey(publicKey) return exchangepartner.do_exchange(pubKey)
10000, dklen=64) nuevosHashes.append(dk.hex()) print( "\n\n Usando sha512 con 10000 rondas, se logro hashear las contraseñas en:\n ", time.clock() - t0) archivo = open("./NuevosHashes/sha3-512_" + str(resultado) + ".txt", "w") for hash in nuevosHashes: archivo.write(str(hash) + "\n") archivo.close() HOST = '127.0.0.1' # The server's hostname or IP address PORT = 65432 # The port used by the server with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT)) s.sendall(b'Hello, world') data = s.recv(1024) data = pickle.loads(data) print('Datos recibidos: ', repr(data)) llavePublica = PublicKey(data) llavePrivada = PrivateKey().do_exchange(llavePublica) print("\n") print(llavePublica) print("\n") print(llavePrivada)