Example #1
0
 def setUp(self):
     self.keyset = KeySet(onion_pub_key=nick_to_pub_key('Alice'),
                          tx_mk=bytes(SYMMETRIC_KEY_LENGTH),
                          rx_mk=bytes(SYMMETRIC_KEY_LENGTH),
                          tx_hk=bytes(SYMMETRIC_KEY_LENGTH),
                          rx_hk=bytes(SYMMETRIC_KEY_LENGTH),
                          tx_harac=INITIAL_HARAC,
                          rx_harac=INITIAL_HARAC,
                          store_keys=lambda: None)
Example #2
0
class TestKeySet(unittest.TestCase):
    def setUp(self):
        self.keyset = KeySet('*****@*****.**', KEY_LENGTH * b'\x00',
                             KEY_LENGTH * b'\x00', KEY_LENGTH * b'\x00',
                             KEY_LENGTH * b'\x00', 0, 0, lambda: None)

    def test_keyset_serialization_length_and_type(self):
        serialized = self.keyset.serialize_k()
        self.assertEqual(len(serialized), KEYSET_LENGTH)
        self.assertIsInstance(serialized, bytes)

    def test_rotate_tx_key(self):
        self.assertIsNone(self.keyset.rotate_tx_key())
        self.assertEqual(self.keyset.tx_key, hash_chain(KEY_LENGTH * b'\x00'))
        self.assertEqual(self.keyset.tx_harac, 1)

    def test_update_tx_key(self):
        self.keyset.update_key(TX, KEY_LENGTH * b'\x01', 2)
        self.assertEqual(self.keyset.tx_key, KEY_LENGTH * b'\x01')
        self.assertEqual(self.keyset.rx_key, KEY_LENGTH * b'\x00')
        self.assertEqual(self.keyset.tx_hek, KEY_LENGTH * b'\x00')
        self.assertEqual(self.keyset.rx_hek, KEY_LENGTH * b'\x00')
        self.assertEqual(self.keyset.tx_harac, 2)

    def test_update_rx_key(self):
        self.keyset.update_key(RX, KEY_LENGTH * b'\x01', 2)
        self.assertEqual(self.keyset.tx_key, KEY_LENGTH * b'\x00')
        self.assertEqual(self.keyset.rx_key, KEY_LENGTH * b'\x01')
        self.assertEqual(self.keyset.tx_hek, KEY_LENGTH * b'\x00')
        self.assertEqual(self.keyset.rx_hek, KEY_LENGTH * b'\x00')
        self.assertEqual(self.keyset.rx_harac, 2)

    def test_invalid_direction_raises_critical_error(self):
        with self.assertRaises(SystemExit):
            self.keyset.update_key('sx', KEY_LENGTH * b'\x01', 2)
Example #3
0
class TestKeySet(unittest.TestCase):
    def setUp(self) -> None:
        """Pre-test actions."""
        self.keyset = KeySet(onion_pub_key=nick_to_pub_key('Alice'),
                             tx_mk=bytes(SYMMETRIC_KEY_LENGTH),
                             rx_mk=bytes(SYMMETRIC_KEY_LENGTH),
                             tx_hk=bytes(SYMMETRIC_KEY_LENGTH),
                             rx_hk=bytes(SYMMETRIC_KEY_LENGTH),
                             tx_harac=INITIAL_HARAC,
                             rx_harac=INITIAL_HARAC,
                             store_keys=lambda: None)

    def test_keyset_serialization_length_and_type(self) -> None:
        serialized = self.keyset.serialize_k()
        self.assertEqual(len(serialized), KEYSET_LENGTH)
        self.assertIsInstance(serialized, bytes)

    def test_rotate_tx_mk(self) -> None:
        self.assertIsNone(self.keyset.rotate_tx_mk())
        self.assertEqual(
            self.keyset.tx_mk,
            blake2b(bytes(SYMMETRIC_KEY_LENGTH) + int_to_bytes(INITIAL_HARAC),
                    digest_size=SYMMETRIC_KEY_LENGTH))
        self.assertEqual(self.keyset.rx_mk, bytes(SYMMETRIC_KEY_LENGTH))
        self.assertEqual(self.keyset.tx_hk, bytes(SYMMETRIC_KEY_LENGTH))
        self.assertEqual(self.keyset.rx_hk, bytes(SYMMETRIC_KEY_LENGTH))
        self.assertEqual(self.keyset.tx_harac, 1)
        self.assertEqual(self.keyset.rx_harac, INITIAL_HARAC)

    def test_update_tx_mk(self) -> None:
        self.keyset.update_mk(TX, SYMMETRIC_KEY_LENGTH * b'\x01', 2)
        self.assertEqual(self.keyset.tx_mk, SYMMETRIC_KEY_LENGTH * b'\x01')
        self.assertEqual(self.keyset.rx_mk, bytes(SYMMETRIC_KEY_LENGTH))
        self.assertEqual(self.keyset.tx_hk, bytes(SYMMETRIC_KEY_LENGTH))
        self.assertEqual(self.keyset.rx_hk, bytes(SYMMETRIC_KEY_LENGTH))
        self.assertEqual(self.keyset.tx_harac, 2)
        self.assertEqual(self.keyset.rx_harac, INITIAL_HARAC)

    def test_update_rx_mk(self) -> None:
        self.keyset.update_mk(RX, SYMMETRIC_KEY_LENGTH * b'\x01', 2)
        self.assertEqual(self.keyset.tx_mk, bytes(SYMMETRIC_KEY_LENGTH))
        self.assertEqual(self.keyset.rx_mk, SYMMETRIC_KEY_LENGTH * b'\x01')
        self.assertEqual(self.keyset.tx_hk, bytes(SYMMETRIC_KEY_LENGTH))
        self.assertEqual(self.keyset.rx_hk, bytes(SYMMETRIC_KEY_LENGTH))
        self.assertEqual(self.keyset.tx_harac, INITIAL_HARAC)
        self.assertEqual(self.keyset.rx_harac, 2)

    def test_invalid_direction_raises_critical_error(self) -> None:
        invalid_direction = 'sx'
        with self.assertRaises(SystemExit):
            self.keyset.update_mk(invalid_direction,
                                  SYMMETRIC_KEY_LENGTH * b'\x01', 2)
Example #4
0
def create_keyset(nick: str,
                  tx_key: bytes = SYMMETRIC_KEY_LENGTH * b'\x01',
                  tx_hek: bytes = SYMMETRIC_KEY_LENGTH * b'\x01',
                  rx_key: bytes = SYMMETRIC_KEY_LENGTH * b'\x01',
                  rx_hek: bytes = SYMMETRIC_KEY_LENGTH * b'\x01',
                  tx_harac: int = INITIAL_HARAC,
                  rx_harac: int = INITIAL_HARAC,
                  store_f: Callable[..., None] = None) -> KeySet:
    """Create a mock keyset object."""
    pub_key = LOCAL_PUBKEY if nick == LOCAL_ID else nick_to_pub_key(nick)
    return KeySet(pub_key, tx_key, tx_hek, rx_key, rx_hek, tx_harac, rx_harac,
                  store_keys=lambda: None if store_f is None else store_f)
Example #5
0
def create_keyset(nick,
                  tx_key=SYMMETRIC_KEY_LENGTH * b'\x01',
                  tx_hek=SYMMETRIC_KEY_LENGTH * b'\x01',
                  rx_key=SYMMETRIC_KEY_LENGTH * b'\x01',
                  rx_hek=SYMMETRIC_KEY_LENGTH * b'\x01',
                  tx_harac=INITIAL_HARAC,
                  rx_harac=INITIAL_HARAC,
                  store_f=None):
    """Create a mock keyset object."""
    pub_key = LOCAL_PUBKEY if nick == LOCAL_ID else nick_to_pub_key(nick)
    return KeySet(pub_key, tx_key, tx_hek, rx_key, rx_hek, tx_harac, rx_harac,
                  store_keys=lambda: None if store_f is None else store_f)
Example #6
0
def create_keyset(nick    ='Alice',
                  tx_key  =KEY_LENGTH * b'\x01',
                  tx_hek  =KEY_LENGTH * b'\x01',
                  rx_key  =KEY_LENGTH * b'\x01',
                  rx_hek  =KEY_LENGTH * b'\x01',
                  tx_harac=INITIAL_HARAC,
                  rx_harac=INITIAL_HARAC,
                  store_f =None):
    """Create mock keyset object."""
    account = LOCAL_ID if nick == LOCAL_ID else f'{nick.lower()}@jabber.org'
    store_f = lambda: None if store_f is None else store_f
    return KeySet(account, tx_key, tx_hek, rx_key, rx_hek, tx_harac, rx_harac, store_f)
Example #7
0
def create_keyset(nick='Alice',
                  tx_key=32 * b'\x01',
                  tx_hek=32 * b'\x01',
                  rx_key=32 * b'\x01',
                  rx_hek=32 * b'\x01',
                  tx_harac=0,
                  rx_harac=0,
                  store_f=None):
    """Create mock keyset object."""
    if store_f is None:
        store_f = lambda: None
    account = 'local' if nick == 'local' else f'{nick.lower()}@jabber.org'
    return KeySet(account, tx_key, tx_hek, rx_key, rx_hek, tx_harac, rx_harac, store_f)
Example #8
0
 def setUp(self):
     self.keyset = KeySet('*****@*****.**', KEY_LENGTH * b'\x00',
                          KEY_LENGTH * b'\x00', KEY_LENGTH * b'\x00',
                          KEY_LENGTH * b'\x00', 0, 0, lambda: None)
Example #9
0
 def add_keyset(self, rx_account, tx_key, rx_key, tx_hek, rx_hek):
     if self.has_keyset(rx_account):
         self.remove_keyset(rx_account)
     self.keysets.append(KeySet(rx_account, tx_key, rx_key, tx_hek, rx_hek, 0, 0, self.store_keys))
     self.store_keys()