Exemplo n.º 1
0
 private static byte[] GenerateTmpGmacKey(GmacInfo gmacInfo, long index)
 {
     if (index != 0)
     {
         return(GenerateGmacKey(gmacInfo.KeyGmacBase, gmacInfo.Iv, index));
     }
     else
     {
         return(gmacInfo.KeyGmacCurrent);
     }
 }
Exemplo n.º 2
0
        /************************/
        /**** private methods ***/
        /************************/

        private static byte[] GmacCrypto(GmacInfo gmacInfo, uint keyPos, byte[] buffer)
        {
            uint padding = keyPos % 16;
            int  size    = (((int)padding + buffer.Length + 16 - 1) / 16) * 16;

            byte[] keyStream = GenerateKeyStream(gmacInfo, keyPos - padding, size);

            byte[] decryptedData = new byte[buffer.Length];
            for (int i = 0; i < buffer.Length; i++)
            {
                decryptedData[i] = (byte)(buffer[i] ^ keyStream[padding + i]);
            }

            return(decryptedData);
        }
Exemplo n.º 3
0
        public static byte[] GenerateKeyStream(GmacInfo gmacInfo, uint keyPos, int keyStreamSize)
        {
            var cipher = CipherUtilities.GetCipher("AES/ECB/NoPadding");

            cipher.Init(true, new KeyParameter(gmacInfo.KeyBase));

            int counterOffset = (int)keyPos / 16;

            byte[] keyStream = new byte[keyStreamSize];
            for (int i = 0; i < keyStreamSize; i += 16)
            {
                byte[] keyStreamPart = new byte[16];
                SetGmacCounter(keyStreamPart, gmacInfo.Iv, counterOffset++);
                Array.Copy(keyStreamPart, 0, keyStream, i, keyStreamPart.Length);
            }

            return(cipher.DoFinal(keyStream));
        }
Exemplo n.º 4
0
        public static byte[] AddGmacToBuffer(GmacInfo gmacInfo, uint keyPos, byte[] buffer, int gmacPositionInBuffer)
        {
            //keyPos = (uint) System.Net.IPAddress.HostToNetworkOrder((int)keyPos);

            byte[] iv = new byte[16];
            SetGmacCounter(iv, gmacInfo.Iv, keyPos / 0x10);

            byte[] gmacKey  = gmacInfo.KeyGmacCurrent;
            long   keyIndex = (keyPos > 0 ? keyPos - 1 : 0) / GmacKeyRefreshKeyPosition;

            if (keyIndex > gmacInfo.GmacCurrentIndex)
            {
                gmacKey = GenerateNewGmacKey(gmacInfo, keyIndex);
                gmacInfo.KeyGmacCurrent   = gmacKey;
                gmacInfo.GmacCurrentIndex = keyIndex;
                gmacKey = gmacInfo.KeyGmacCurrent;
            }
            else if (keyIndex < gmacInfo.GmacCurrentIndex)
            {
                gmacKey = GenerateTmpGmacKey(gmacInfo, keyIndex);
            }

            IMac mac = new GMac(new GcmBlockCipher(new AesEngine()), 32);
            ICipherParameters key = new KeyParameter(gmacKey);

            mac.Init(new ParametersWithIV(key, iv));
            mac.BlockUpdate(buffer, 0, buffer.Length);
            int macSize = mac.GetMacSize();

            byte[] gmac = new byte[macSize];
            mac.DoFinal(gmac, 0);

            for (int i = 0; i < gmac.Length; i++)
            {
                buffer[gmacPositionInBuffer + i] = gmac[i];
            }

            return(gmac);
        }
Exemplo n.º 5
0
 public static byte[] GenerateNewGmacKey(GmacInfo gmacInfo, long index)
 {
     return(GenerateGmacKey(gmacInfo.KeyGmacBase, gmacInfo.Iv, index));
 }
Exemplo n.º 6
0
 public static byte[] DecryptGmacMessage(GmacInfo gmacInfo, uint keyPos, byte[] buffer)
 {
     return(GmacCrypto(gmacInfo, keyPos, buffer));
 }