Esempio n. 1
0
    def q_key(k: bytes):

        #part 1
        left_part = ByteUtils.getLeftPart(k)
        sqr_1 = FFunc.f_func(left_part, Key.S_1)
        right_part = ByteUtils.xor(sqr_1, ByteUtils.getRightPart(k))

        #part 2
        left_part, right_part = right_part, left_part
        sqr_2 = FFunc.f_func(left_part, Key.S_2)
        right_part = ByteUtils.xor(sqr_2, right_part)

        #part 3
        left_part, right_part = right_part, left_part
        part_3_res = ByteUtils.xor(left_part + right_part, k)

        #part 4
        left_part = ByteUtils.getLeftPart(part_3_res)
        right_part = ByteUtils.getRightPart(part_3_res)
        sqr_3 = FFunc.f_func(left_part, Key.S_3)
        right_part = ByteUtils.xor(sqr_3, right_part)

        #part 5
        left_part, right_part = right_part, left_part
        sqr_4 = FFunc.f_func(left_part, Key.S_4)
        right_part = ByteUtils.xor(sqr_4, right_part)

        return right_part + left_part
Esempio n. 2
0
    def decrypt(x: bytes, key: bytes):
        if (len(x) < 16):
            raise Exception("text should contain more or equal than 16 bytes")
        elif (len(key) != 16):
            raise Exception("text should have 16 bytes")
        elif (len(x) % 16 != 0):
            raise Exception("text should have total each block")

        count = len(x) // 16
        blocks = []

        for i in range(1, count + 1):
            blocks.append(x[16 * (i - 1): 16 * i])

        cipher_blocks = []
        init_block = key
        for i in range(len(blocks)):
            cipher_block = ByteUtils.xor(blocks[i], init_block)
            cipher_blocks.append(cipher_block)
            init_block = blocks[i]

        result = bytes()
        for block in cipher_blocks:
            result += block

        if (CBC.is_block_empty(CBC.get_16_bytes_block(result, 1))):
            return CBC.handle_decrypt_if_last_block_empty(result)
        else:
            return result
Esempio n. 3
0
    def encrypt_if_block_not_total(x: bytes, key: bytes):
        count = len(x) // 16
        blocks = []

        for i in range(1, count + 1):
            blocks.append(x[16 * (i - 1): 16 * i])

        last_bloc = x[16 * count: len(x)]

        if(len(last_bloc) == 16):
            blocks.append(last_bloc)
        else:
            empty_bytes = 0
            while(len(last_bloc) < 15):
                empty_bytes += 1
                last_bloc += int(0).to_bytes(1, "little")
            last_bloc += int(empty_bytes).to_bytes(1, "little")
            blocks.append(last_bloc)
            blocks.append(int(0).to_bytes(16, "little"))

        cipher_blocks = []
        init_block = key
        for i in range(len(blocks)):
            cipher_block = ByteUtils.xor(blocks[i], init_block)
            cipher_blocks.append(cipher_block)
            init_block = cipher_block

        result = bytes()
        for block in cipher_blocks:
            result += block

        return result
Esempio n. 4
0
    def fl_inv(y: bytes, kl: bytes):
        y_l = ByteUtils.getLeftPart(y)
        y_r = ByteUtils.getRightPart(y)
        kl_l = ByteUtils.getLeftPart(kl)
        kl_R = ByteUtils.getRightPart(kl)

        sqr_1 = (int.from_bytes(y_r, "little")
                 | int.from_bytes(kl_R, "little")).to_bytes(4, "little")
        sqr_2 = ByteUtils.xor(sqr_1, y_l)
        sqr_3 = (int.from_bytes(sqr_2, "little")
                 & int.from_bytes(kl_l, "little")).to_bytes(4, "little")
        sqr_4 = ByteUtils.rol(sqr_3, 1)
        sqr_5 = ByteUtils.xor(sqr_4, y_r)

        return sqr_2 + sqr_5
Esempio n. 5
0
    def fl_func(x: bytes, kl: bytes):
        x_l = ByteUtils.getLeftPart(x)
        x_r = ByteUtils.getRightPart(x)
        kl_l = ByteUtils.getLeftPart(kl)
        kl_r = ByteUtils.getRightPart(kl)

        sqr_1 = int(
            int.from_bytes(kl_l, "little")
            & int.from_bytes(x_l, "little")).to_bytes(4, "little")
        sqr_2 = ByteUtils.rol(sqr_1, 1)
        sqr_3 = ByteUtils.xor(sqr_2, x_r)
        sqr_4 = (int.from_bytes(sqr_3, "little")
                 | int.from_bytes(kl_r, "little")).to_bytes(4, "little")
        sqr_5 = ByteUtils.xor(sqr_4, x_l)

        return sqr_5 + sqr_3
Esempio n. 6
0
    def encrypt(x: bytes, key: bytes):
        if (len(x) != 16):
            raise Exception("entry value should be 16 bytes")
        elif (len(key) != 16):
            raise Exception("key should be 16 bytes")

        Camelia.init_key_k(key)

        left = ByteUtils.xor(ByteUtils.getLeftPart(x), Camelia.kw_i[0])
        right = ByteUtils.xor(ByteUtils.getRightPart(x), Camelia.kw_i[1])

        for i in range(6):
            sqr = FFunc.f_func(left, Camelia.k_i[i])
            right = ByteUtils.xor(right, sqr)
            right, left = left, right

        left = FLFunction.fl_func(left, Camelia.kl_i[0])
        right = FLFunction.fl_inv(right, Camelia.kl_i[1])

        for i in range(6, 12):
            sqr = FFunc.f_func(left, Camelia.k_i[i])
            right = ByteUtils.xor(right, sqr)
            right, left = left, right

        left = FLFunction.fl_func(left, Camelia.kl_i[2])
        right = FLFunction.fl_inv(right, Camelia.kl_i[3])

        for i in range(12, 18):
            sqr = FFunc.f_func(left, Camelia.k_i[i])
            right = ByteUtils.xor(right, sqr)
            right, left = left, right

        left, right = ByteUtils.xor(right, Camelia.kw_i[2]), ByteUtils.xor(
            left, Camelia.kw_i[3])

        return left + right
Esempio n. 7
0
    def encrypt_if_block_total(x: bytes, key: bytes):
        count = len(x) // 16
        blocks = []

        for i in range(1, count + 1):
            blocks.append(x[16 * (i - 1): 16 * i])

        cipher_blocks = []
        init_block = key
        for i in range(len(blocks)):
            cipher_block = ByteUtils.xor(blocks[i], init_block)
            cipher_blocks.append(cipher_block)
            init_block = cipher_block

        result = bytes()
        for block in cipher_blocks:
            result += block

        return result
Esempio n. 8
0
    def init_key_k(key: bytes):
        key_q = Key.q_key(key)

        Camelia.k_i = [
            ByteUtils.getLeftPart(key_q),
            ByteUtils.getRightPart(key_q),
            ByteUtils.getLeftPart(ByteUtils.rol(key, 15)),
            ByteUtils.getRightPart(ByteUtils.rol(key, 15)),
            ByteUtils.getLeftPart(ByteUtils.rol(key_q, 15)),
            ByteUtils.getRightPart(ByteUtils.rol(key_q, 15)),
            ByteUtils.getLeftPart(ByteUtils.rol(key, 45)),
            ByteUtils.getRightPart(ByteUtils.rol(key, 45)),
            ByteUtils.getLeftPart(ByteUtils.rol(key_q, 45)),
            ByteUtils.getRightPart(ByteUtils.rol(key, 60)),
            ByteUtils.getLeftPart(ByteUtils.rol(key_q, 60)),
            ByteUtils.getRightPart(ByteUtils.rol(key_q, 60)),
            ByteUtils.getLeftPart(ByteUtils.rol(key, 94)),
            ByteUtils.getRightPart(ByteUtils.rol(key, 94)),
            ByteUtils.getLeftPart(ByteUtils.rol(key_q, 94)),
            ByteUtils.getRightPart(ByteUtils.rol(key_q, 94)),
            ByteUtils.getLeftPart(ByteUtils.rol(key, 111)),
            ByteUtils.getRightPart(ByteUtils.rol(key, 111)),
        ]

        Camelia.kl_i = [
            ByteUtils.getLeftPart(ByteUtils.rol(key_q, 30)),
            ByteUtils.getRightPart(ByteUtils.rol(key_q, 30)),
            ByteUtils.getLeftPart(ByteUtils.rol(key, 77)),
            ByteUtils.getRightPart(ByteUtils.rol(key, 77)),
        ]

        Camelia.kw_i = [
            ByteUtils.getLeftPart(key),
            ByteUtils.getRightPart(key),
            ByteUtils.getLeftPart(ByteUtils.rol(key_q, 111)),
            ByteUtils.getRightPart(ByteUtils.rol(key_q, 111))
        ]
Esempio n. 9
0
 def test_ror(self):
     self.assertEqual(self.res_ror,
                      ByteUtils.ror(self.bytes_, self.count_rol))
Esempio n. 10
0
 def test_xor(self):
     self.assertEqual(self.res_xor.hex(),
                      ByteUtils.xor(self.bytes_, self.other_xor).hex())
Esempio n. 11
0
 def test_get_R(self):
     self.assertEqual(self.right, ByteUtils.getRightPart(self.bytes_))
Esempio n. 12
0
 def test_get_L(self):
     self.assertEqual(self.left, ByteUtils.getLeftPart(self.bytes_))
Esempio n. 13
0
 def s4(x: bytes):
     rol_x = ByteUtils.rol(x, 1)
     s1_bytes = FFunc.s1_arr[int.from_bytes(rol_x, "little")].to_bytes(
         1, "little")
     return s1_bytes
Esempio n. 14
0
 def s3(x: bytes):
     s1_bytes = FFunc.s1_arr[int.from_bytes(x, "little")].to_bytes(
         1, "little")
     return ByteUtils.ror(s1_bytes, 1)