2015-03-20 8 views
2

Я знаю, что MAC - это 4 первых байта последнего блочного шифрования, и нашел это объяснение CMAC here, но это очень трудно понять. И, возможно, уже есть какие-то вопросы CMAC AES, но я сожалею, что не могу понять это хорошо.Расчет AES CMAC C#

Любой может объяснить, как вычислить CMAC? и при необходимости с некоторым примером кода на C#. Спасибо

+1

32-разрядный тег (4 байта), вероятно, слишком мал, чтобы обеспечить значимую защиту для нетривиального временного окна. Вероятно, вы должны использовать 64-бит или выше. Рекомендуется использовать 96-бит и выше. Вы также можете посетить статью [Аутентифицированное шифрование] (https://www.cryptopp.com/wiki/Authenticated_Encryption) из вики-библиотеки библиотеки безопасности. – jww

+0

В вашем случае, вероятно, лучше использовать режим CCM, EAX или GCM. Это освобождает вас от объединения режима конфиденциальности с режимом аутентичности. Вы устанавливаете ключ и iv, а затем вещи «просто работают» для вас. – jww

+0

Да, я увеличу его до 96-битного, также аутентифицированное шифрование отлично, спасибо! –

ответ

11

Сначала вам нужно получить два подраздела с вашего ключа AES. Алгоритм хорошо описан в RFC4493, но здесь я приведу примеры кода. Для этого вам понадобится функция AESEncrypt, которую вы можете написать с помощью Dotnet AesCryptoServiceProvider:

byte[] AESEncrypt(byte[] key, byte[] iv, byte[] data) 
    { 
     using (MemoryStream ms = new MemoryStream()) 
     { 
      AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); 

      aes.Mode = CipherMode.CBC; 
      aes.Padding = PaddingMode.None; 

      using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(key, iv), CryptoStreamMode.Write)) 
      { 
       cs.Write(data, 0, data.Length); 
       cs.FlushFinalBlock(); 

       return ms.ToArray(); 
      } 
     } 
    } 

И что-то сдвинуть массивы влево на один бит:

byte[] Rol(byte[] b) 
    { 
     byte[] r = new byte[b.Length]; 
     byte carry = 0; 

     for (int i = b.Length - 1; i >= 0; i--) 
     { 
      ushort u = (ushort)(b[i] << 1); 
      r[i] = (byte)((u & 0xff) + carry); 
      carry = (byte)((u & 0xff00) >> 8); 
     } 

     return r; 
    } 

Теперь только реализацию алгоритма в RFC4493 остается. Я прокомментировал логику из RFC для более легкого понимания.

byte[] AESCMAC(byte[] key, byte[] data) 
    { 
     // SubKey generation 
     // step 1, AES-128 with key K is applied to an all-zero input block. 
     byte[] L = AESEncrypt(key, new byte[16], new byte[16]); 

     // step 2, K1 is derived through the following operation: 
     byte[] FirstSubkey = Rol(L); //If the most significant bit of L is equal to 0, K1 is the left-shift of L by 1 bit. 
     if ((L[0] & 0x80) == 0x80) 
      FirstSubkey[15] ^= 0x87; // Otherwise, K1 is the exclusive-OR of const_Rb and the left-shift of L by 1 bit. 

     // step 3, K2 is derived through the following operation: 
     byte[] SecondSubkey = Rol(FirstSubkey); // If the most significant bit of K1 is equal to 0, K2 is the left-shift of K1 by 1 bit. 
     if ((FirstSubkey[0] & 0x80) == 0x80) 
      SecondSubkey[15] ^= 0x87; // Otherwise, K2 is the exclusive-OR of const_Rb and the left-shift of K1 by 1 bit. 

     // MAC computing 
     if (((data.Length != 0) && (data.Length % 16 == 0)) == true) 
     { 
      // If the size of the input message block is equal to a positive multiple of the block size (namely, 128 bits), 
      // the last block shall be exclusive-OR'ed with K1 before processing 
      for (int j = 0; j < FirstSubkey.Length; j++) 
       data[data.Length - 16 + j] ^= FirstSubkey[j]; 
     } 
     else 
     { 
      // Otherwise, the last block shall be padded with 10^i 
      byte[] padding = new byte[16 - data.Length % 16]; 
      padding[0] = 0x80; 

      data = data.Concat<byte>(padding.AsEnumerable()).ToArray(); 

      // and exclusive-OR'ed with K2 
      for (int j = 0; j < SecondSubkey.Length; j++) 
       data[data.Length - 16 + j] ^= SecondSubkey[j]; 
     } 

     // The result of the previous process will be the input of the last encryption. 
     byte[] encResult = AESEncrypt(key, new byte[16], data); 

     byte[] HashValue = new byte[16]; 
     Array.Copy(encResult, encResult.Length - HashValue.Length, HashValue, 0, HashValue.Length); 

     return HashValue; 
    } 

Удачи вам!