def test_01_expand(self):
        """expand_des_key()"""
        from passlib.crypto.des import (
            expand_des_key,
            shrink_des_key,
            _KDATA_MASK,
            INT_56_MASK,
        )

        # make sure test vectors are preserved (sans parity bits)
        # uses ints, bytes are tested under # 02
        for key1, _, _ in self.des_test_vectors:
            key2 = shrink_des_key(key1)
            key3 = expand_des_key(key2)
            # NOTE: this assumes expand_des_key() sets parity bits to 0
            self.assertEqual(key3, key1 & _KDATA_MASK)

        # type checks
        self.assertRaises(TypeError, expand_des_key, 1.0)

        # too large
        self.assertRaises(ValueError, expand_des_key, INT_56_MASK + 1)
        self.assertRaises(ValueError, expand_des_key, b"\x00" * 8)

        # too small
        self.assertRaises(ValueError, expand_des_key, -1)
        self.assertRaises(ValueError, expand_des_key, b"\x00" * 6)
Beispiel #2
0
def mdes_encrypt_int_block(
        key,
        input,
        salt=0,
        rounds=1):  # pragma: no cover -- deprecated & unused
    if isinstance(key, bytes):
        if len(key) == 7:
            key = expand_des_key(key)
        key = _unpack_uint64(key)[0]
    return des_encrypt_int_block(key, input, salt, rounds)
Beispiel #3
0
    def test_02_shrink(self):
        """shrink_des_key()"""
        from passlib.crypto.des import expand_des_key, shrink_des_key, INT_64_MASK
        rng = self.getRandom()

        # make sure reverse works for some random keys
        # uses bytes, ints are tested under # 01
        for i in range(20):
            key1 = getrandbytes(rng, 7)
            key2 = expand_des_key(key1)
            key3 = shrink_des_key(key2)
            self.assertEqual(key3, key1)

        # type checks
        self.assertRaises(TypeError, shrink_des_key, 1.0)

        # too large
        self.assertRaises(ValueError, shrink_des_key, INT_64_MASK+1)
        self.assertRaises(ValueError, shrink_des_key, b"\x00"*9)

        # too small
        self.assertRaises(ValueError, shrink_des_key, -1)
        self.assertRaises(ValueError, shrink_des_key, b"\x00"*7)
Beispiel #4
0
    def test_01_expand(self):
        """expand_des_key()"""
        from passlib.crypto.des import expand_des_key, shrink_des_key, \
                                             _KDATA_MASK, INT_56_MASK

        # make sure test vectors are preserved (sans parity bits)
        # uses ints, bytes are tested under # 02
        for key1, _, _ in self.des_test_vectors:
            key2 = shrink_des_key(key1)
            key3 = expand_des_key(key2)
            # NOTE: this assumes expand_des_key() sets parity bits to 0
            self.assertEqual(key3, key1 & _KDATA_MASK)

        # type checks
        self.assertRaises(TypeError, expand_des_key, 1.0)

        # too large
        self.assertRaises(ValueError, expand_des_key, INT_56_MASK+1)
        self.assertRaises(ValueError, expand_des_key, b"\x00"*8)

        # too small
        self.assertRaises(ValueError, expand_des_key, -1)
        self.assertRaises(ValueError, expand_des_key, b"\x00"*6)
    def test_02_shrink(self):
        """shrink_des_key()"""
        from passlib.crypto.des import expand_des_key, shrink_des_key, INT_64_MASK
        rng = self.getRandom()

        # make sure reverse works for some random keys
        # uses bytes, ints are tested under # 01
        for i in range(20):
            key1 = getrandbytes(rng, 7)
            key2 = expand_des_key(key1)
            key3 = shrink_des_key(key2)
            self.assertEqual(key3, key1)

        # type checks
        self.assertRaises(TypeError, shrink_des_key, 1.0)

        # too large
        self.assertRaises(ValueError, shrink_des_key, INT_64_MASK + 1)
        self.assertRaises(ValueError, shrink_des_key, b"\x00" * 9)

        # too small
        self.assertRaises(ValueError, shrink_des_key, -1)
        self.assertRaises(ValueError, shrink_des_key, b"\x00" * 7)
Beispiel #6
0
def mdes_encrypt_int_block(key, input, salt=0, rounds=1): # pragma: no cover -- deprecated & unused
    if isinstance(key, bytes):
        if len(key) == 7:
            key = expand_des_key(key)
        key = _unpack_uint64(key)[0]
    return des_encrypt_int_block(key, input, salt, rounds)