/// <summary> /// /// </summary> /// <param name="data"></param> /// <param name="key"></param> /// <returns></returns> public static byte[] ComputePayPassMac(this byte[] data, KeyParameter key) { IBlockCipher cipher = new DesEdeEngine(); IMac mac = new CbcBlockCipherMac(cipher, 64, new ISO7816d4Padding()); mac.Init(key); mac.BlockUpdate(data, 0, data.Length); var m = new byte[mac.GetMacSize()]; mac.DoFinal(m, 0); return(m); }
private int calculateMac(byte[] data, int dataOff, int dataLen, byte[] macBlock) { IMac cMac = new CbcBlockCipherMac(cipher, macSize * 8); cMac.Init(keyParam); // // build b0 // byte[] b0 = new byte[16]; if (HasAssociatedText()) { b0[0] |= 0x40; } b0[0] |= (byte)((((cMac.GetMacSize() - 2) / 2) & 0x7) << 3); b0[0] |= (byte)(((15 - nonce.Length) - 1) & 0x7); Array.Copy(nonce, 0, b0, 1, nonce.Length); int q = dataLen; int count = 1; while (q > 0) { b0[b0.Length - count] = (byte)(q & 0xff); q >>= 8; count++; } cMac.BlockUpdate(b0, 0, b0.Length); // // process associated text // if (HasAssociatedText()) { int extra; int textLength = GetAssociatedTextLength(); if (textLength < ((1 << 16) - (1 << 8))) { cMac.Update((byte)(textLength >> 8)); cMac.Update((byte)textLength); extra = 2; } else // can't go any higher than 2^32 { cMac.Update((byte)0xff); cMac.Update((byte)0xfe); cMac.Update((byte)(textLength >> 24)); cMac.Update((byte)(textLength >> 16)); cMac.Update((byte)(textLength >> 8)); cMac.Update((byte)textLength); extra = 6; } if (initialAssociatedText != null) { cMac.BlockUpdate(initialAssociatedText, 0, initialAssociatedText.Length); } if (associatedText.Position > 0) { cMac.BlockUpdate(associatedText.ToArray() /*GetBuffer()*/, 0, (int)associatedText.Position); } extra = (extra + textLength) % 16; if (extra != 0) { for (int i = extra; i < 16; ++i) { cMac.Update((byte)0x00); } } } // // add the text // cMac.BlockUpdate(data, dataOff, dataLen); return(cMac.DoFinal(macBlock, 0)); }
private int calculateMac(byte[] data, int dataOff, int dataLen, byte[] macBlock) { IMac mac = new CbcBlockCipherMac(this.cipher, this.macSize * 8); mac.Init(this.keyParam); byte[] array = new byte[16]; if (this.HasAssociatedText()) { byte[] expr_37_cp_0 = array; int expr_37_cp_1 = 0; expr_37_cp_0[expr_37_cp_1] |= 64; } byte[] expr_4D_cp_0 = array; int expr_4D_cp_1 = 0; expr_4D_cp_0[expr_4D_cp_1] |= (byte)(((mac.GetMacSize() - 2) / 2 & 7) << 3); byte[] expr_70_cp_0 = array; int expr_70_cp_1 = 0; expr_70_cp_0[expr_70_cp_1] |= (byte)(15 - this.nonce.Length - 1 & 7); Array.Copy(this.nonce, 0, array, 1, this.nonce.Length); int i = dataLen; int num = 1; while (i > 0) { array[array.Length - num] = (byte)(i & 255); i >>= 8; num++; } mac.BlockUpdate(array, 0, array.Length); if (this.HasAssociatedText()) { int associatedTextLength = this.GetAssociatedTextLength(); int num2; if (associatedTextLength < 65280) { mac.Update((byte)(associatedTextLength >> 8)); mac.Update((byte)associatedTextLength); num2 = 2; } else { mac.Update(255); mac.Update(254); mac.Update((byte)(associatedTextLength >> 24)); mac.Update((byte)(associatedTextLength >> 16)); mac.Update((byte)(associatedTextLength >> 8)); mac.Update((byte)associatedTextLength); num2 = 6; } if (this.initialAssociatedText != null) { mac.BlockUpdate(this.initialAssociatedText, 0, this.initialAssociatedText.Length); } if (this.associatedText.Position > 0L) { mac.BlockUpdate(this.associatedText.GetBuffer(), 0, (int)this.associatedText.Position); } num2 = (num2 + associatedTextLength) % 16; if (num2 != 0) { for (int j = num2; j < 16; j++) { mac.Update(0); } } } mac.BlockUpdate(data, dataOff, dataLen); return(mac.DoFinal(macBlock, 0)); }
private int calculateMac(byte[] data, int dataOff, int dataLen, byte[] macBlock) { IMac cMac = new CbcBlockCipherMac(cipher, parameters.MacSize); byte[] nonce = parameters.GetNonce(); byte[] associatedText = parameters.GetAssociatedText(); cMac.Init(parameters.Key); // // build b0 // byte[] b0 = new byte[16]; if (associatedText != null && associatedText.Length != 0) { b0[0] |= 0x40; } b0[0] |= (byte)((((cMac.GetMacSize() - 2) / 2) & 0x7) << 3); b0[0] |= (byte)(((15 - nonce.Length) - 1) & 0x7); Array.Copy(nonce, 0, b0, 1, nonce.Length); int q = dataLen; int count = 1; while (q > 0) { b0[b0.Length - count] = (byte)(q & 0xff); q >>= 8; count++; } cMac.BlockUpdate(b0, 0, b0.Length); // // process associated text // if (associatedText != null) { int extra; if (associatedText.Length < ((1 << 16) - (1 << 8))) { cMac.Update((byte)(associatedText.Length >> 8)); cMac.Update((byte)associatedText.Length); extra = 2; } else // can't go any higher than 2^32 { cMac.Update((byte)0xff); cMac.Update((byte)0xfe); cMac.Update((byte)(associatedText.Length >> 24)); cMac.Update((byte)(associatedText.Length >> 16)); cMac.Update((byte)(associatedText.Length >> 8)); cMac.Update((byte)associatedText.Length); extra = 6; } cMac.BlockUpdate(associatedText, 0, associatedText.Length); extra = (extra + associatedText.Length) % 16; if (extra != 0) { for (int i = 0; i != 16 - extra; i++) { cMac.Update((byte)0x00); } } } // // add the text // cMac.BlockUpdate(data, dataOff, dataLen); return(cMac.DoFinal(macBlock, 0)); }
private int calculateMac(byte[] data, int dataOff, int dataLen, byte[] macBlock) { IMac mac = new CbcBlockCipherMac(cipher, macSize * 8); mac.Init(keyParam); byte[] array = new byte[16]; if (HasAssociatedText()) { array[0] |= 64; } array[0] |= (byte)((((mac.GetMacSize() - 2) / 2) & 7) << 3); array[0] |= (byte)((15 - nonce.Length - 1) & 7); Array.Copy(nonce, 0, array, 1, nonce.Length); int num = dataLen; int num2 = 1; while (num > 0) { array[array.Length - num2] = (byte)(num & 0xFF); num >>= 8; num2++; } mac.BlockUpdate(array, 0, array.Length); if (HasAssociatedText()) { int associatedTextLength = GetAssociatedTextLength(); int num3; if (associatedTextLength < 65280) { mac.Update((byte)(associatedTextLength >> 8)); mac.Update((byte)associatedTextLength); num3 = 2; } else { mac.Update(byte.MaxValue); mac.Update(254); mac.Update((byte)(associatedTextLength >> 24)); mac.Update((byte)(associatedTextLength >> 16)); mac.Update((byte)(associatedTextLength >> 8)); mac.Update((byte)associatedTextLength); num3 = 6; } if (initialAssociatedText != null) { mac.BlockUpdate(initialAssociatedText, 0, initialAssociatedText.Length); } if (associatedText.Position > 0) { mac.BlockUpdate(associatedText.ToArray(), 0, (int)associatedText.Position); } num3 = (num3 + associatedTextLength) % 16; if (num3 != 0) { for (int i = num3; i < 16; i++) { mac.Update(0); } } } mac.BlockUpdate(data, dataOff, dataLen); return(mac.DoFinal(macBlock, 0)); }
private int CalculateMac(byte[] data, int dataOff, int dataLen, byte[] macBlock) { IMac mac = new CbcBlockCipherMac(this.cipher, this.macSize * 8); mac.Init(this.keyParam); byte[] destinationArray = new byte[0x10]; if (this.HasAssociatedText()) { destinationArray[0] = (byte)(destinationArray[0] | 0x40); } destinationArray[0] = (byte)(destinationArray[0] | ((byte)((((mac.GetMacSize() - 2) / 2) & 7) << 3))); destinationArray[0] = (byte)(destinationArray[0] | ((byte)(((15 - this.nonce.Length) - 1) & 7))); Array.Copy(this.nonce, 0, destinationArray, 1, this.nonce.Length); int num = dataLen; for (int i = 1; num > 0; i++) { destinationArray[destinationArray.Length - i] = (byte)(num & 0xff); num = num >> 8; } mac.BlockUpdate(destinationArray, 0, destinationArray.Length); if (this.HasAssociatedText()) { int num3; int associatedTextLength = this.GetAssociatedTextLength(); if (associatedTextLength < 0xff00) { mac.Update((byte)(associatedTextLength >> 8)); mac.Update((byte)associatedTextLength); num3 = 2; } else { mac.Update(0xff); mac.Update(0xfe); mac.Update((byte)(associatedTextLength >> 0x18)); mac.Update((byte)(associatedTextLength >> 0x10)); mac.Update((byte)(associatedTextLength >> 8)); mac.Update((byte)associatedTextLength); num3 = 6; } if (this.initialAssociatedText != null) { mac.BlockUpdate(this.initialAssociatedText, 0, this.initialAssociatedText.Length); } if (this.associatedText.Position > 0L) { byte[] buffer = this.associatedText.GetBuffer(); int position = (int)this.associatedText.Position; mac.BlockUpdate(buffer, 0, position); } num3 = (num3 + associatedTextLength) % 0x10; if (num3 != 0) { for (int j = num3; j < 0x10; j++) { mac.Update(0); } } } mac.BlockUpdate(data, dataOff, dataLen); return(mac.DoFinal(macBlock, 0)); }
private int CalculateMac(byte[] data, int dataOff, int dataLen, byte[] macBlock) { IMac mac = new CbcBlockCipherMac(cipher, macSize * 8); mac.Init(keyParam); byte[] array = new byte[16]; byte[] array2; if (HasAssociatedText()) { (array2 = array)[0] = (byte)(array2[0] | 0x40u); } (array2 = array)[0] = (byte)(array2[0] | (byte)((((mac.GetMacSize() - 2) / 2) & 7) << 3)); (array2 = array)[0] = (byte)(array2[0] | (byte)((uint)(15 - nonce.Length - 1) & 7u)); global::System.Array.Copy((global::System.Array)nonce, 0, (global::System.Array)array, 1, nonce.Length); int num = dataLen; int num2 = 1; while (num > 0) { array[array.Length - num2] = (byte)((uint)num & 0xFFu); num >>= 8; num2++; } mac.BlockUpdate(array, 0, array.Length); if (HasAssociatedText()) { int associatedTextLength = GetAssociatedTextLength(); int num3; if (associatedTextLength < 65280) { mac.Update((byte)(associatedTextLength >> 8)); mac.Update((byte)associatedTextLength); num3 = 2; } else { mac.Update(255); mac.Update(254); mac.Update((byte)(associatedTextLength >> 24)); mac.Update((byte)(associatedTextLength >> 16)); mac.Update((byte)(associatedTextLength >> 8)); mac.Update((byte)associatedTextLength); num3 = 6; } if (initialAssociatedText != null) { mac.BlockUpdate(initialAssociatedText, 0, initialAssociatedText.Length); } if (((Stream)associatedText).get_Position() > 0) { byte[] buffer = associatedText.GetBuffer(); int len = (int)((Stream)associatedText).get_Position(); mac.BlockUpdate(buffer, 0, len); } num3 = (num3 + associatedTextLength) % 16; if (num3 != 0) { for (int i = num3; i < 16; i++) { mac.Update(0); } } } mac.BlockUpdate(data, dataOff, dataLen); return(mac.DoFinal(macBlock, 0)); }