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
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
def test_s4(self): self.assertEqual(bytes.fromhex("C0"), FFunc.s4(bytes.fromhex("03")))
def test_s3(self): self.assertEqual(bytes.fromhex("76"), FFunc.s3(bytes.fromhex("03")))
def test_s2(self): self.assertEqual(bytes.fromhex("D9"), FFunc.s2(bytes.fromhex("03")))
def test_s1(self): self.assertEqual(bytes.fromhex("EC"), FFunc.s1(bytes.fromhex("03")))
def test_f_func(self): self.assertEqual(FFunc.f_func(self.x, self.key), self.f_result)