Python Implementation of the CMAC Algorithm not working

154 Views Asked by At

I need to implement the CMAC algorithm for my thesis, however I am getting the wrong result and can simply not recognise where the error is.

The Generation of Subkey K1 and the AES Encryption algorithm, as well as the XOR and the composition of m_i are all correct, I have double checked that.

Can someone please help me out?

there is only one block of size 128 bits (16 octets), thus 'flag' is true and 'n' = 1

The steps I am trying to replicate are:

   +                                                                   +
   +   Step 4.  if flag is true                                        +
   +            then M_last := M_n XOR K1;                             +
   +            else M_last := padding(M_n) XOR K2;                    +
   +   Step 5.  X := const_Zero;                                       +
   +   Step 6.  for i := 1 to n-1 do                                   +
   +                begin                                              +
   +                  Y := X XOR M_i;                                  +
   +                  X := AES-128(K,Y);                               +
   +                end                                                +
   +            Y := M_last XOR X;                                     +
   +            T := AES-128(K,Y);                                     +
   +   Step 7.  return T;                                              +

and my implementation is this:

def generate_cmac(master_key, message_counter, meter_id, key1):

    padding = [0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7, 0, 7]

    m_i = AES_Decryption.hexadecimal_to_binary([0, 0])
    m_i.extend(message_counter)
    m_i.extend(meter_id)
    m_i.extend(AES_Decryption.hexadecimal_to_binary(padding))

    print(AES_Decryption.binary_to_hexadecimal(m_i))

    m_last = AES_Decryption.xor(m_i, key1, 'bytewise')

    init_vector = AES_Decryption.hexadecimal_to_binary(
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

    variable_x = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

    variable_y = AES_Decryption.xor(m_last, variable_x, 'bytewise')

    k_enc = AES_Decryption.aes_encryption(variable_y, master_key, init_vector)

    print(AES_Decryption.binary_to_hexadecimal(k_enc))
0

There are 0 best solutions below