Exemple #1
0
 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.')
Exemple #2
0
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.')
Exemple #3
0
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.'
        )
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)
print("ECDSA P256 with SHA256\n")