Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        public static byte[] BouncyCastleGetCbcMac(string data, byte[] macKey)
        {
            var macBytes      = new byte[4];
            var macCipher     = new CbcBlockCipherMac(new DesEdeEngine());
            var keyParameters = new DesEdeParameters(macKey);

            //var engine = new DesEdeEngine();
            //engine.Init(true, keyParameters);
            macCipher.Init(keyParameters);
            var dataBytes = Encoding.UTF8.GetBytes(data);

            macCipher.BlockUpdate(dataBytes, 0, data.Length);
            macCipher.DoFinal(macBytes, 0);
            return(macBytes);
        }
        public static String getMacValue(String macCipherText, byte[] macKey)
        {
            byte[] macBytes = new byte[4];

            CbcBlockCipherMac cipher        = new CbcBlockCipherMac(new DesEdeEngine());
            DesEdeParameters  keyParameters = new DesEdeParameters(macKey);
            DesEdeEngine      engine        = new DesEdeEngine();

            engine.Init(true, keyParameters);
            cipher.Init(keyParameters);
            byte[] macDataBytes = Encoding.UTF8.GetBytes(macCipherText);
            cipher.BlockUpdate(macDataBytes, 0, macCipherText.Length);
            cipher.DoFinal(macBytes, 0);
            byte[] encodedMacBytes = Hex.Encode(macBytes);
            String mac             = Encoding.Default.GetString(encodedMacBytes);

            return(mac);
        }
Beispiel #4
0
        } //private byte[] CalculateDesMac(byte[] input, byte[] icv)

        private byte[] CalculateAESMac(byte[] input, byte[] icv, int macType = 0)
        {
            if (icv == null)
            {
                icv = new byte[16];
            }

            IMac mac = new CbcBlockCipherMac(new AesEngine());

            mac.Init(new ParametersWithIV(new KeyParameter(theKey), icv));

            mac.BlockUpdate(input, 0, input.Length);

            byte[] result = new byte[16];
            int    outL   = mac.DoFinal(result, 0);

            if (outL > 0)
            {
                log.Debug(outL.ToString());
            }

            return(result);
        } //private byte[] CalculateAESMac(byte[] input, byte[] icv)
        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));
        }
        protected byte[] AES_CBC_MAC(CBORObject alg, byte[] K)
        {
            int cbitKey;
            int cbitTag;
            //  Defaults to PKCS#7

            IBlockCipher aes = new AesFastEngine();

            KeyParameter ContentKey;

            //  The requirements from spec
            //  IV is 128 bits of zeros
            //  key sizes are 128, 192 and 256 bits
            //  Authentication tag sizes are 64 and 128 bits

            byte[] IV = new byte[128 / 8];

            Debug.Assert(alg.Type == CBORType.Number);
            switch ((AlgorithmValuesInt)alg.AsInt32())
            {
            case AlgorithmValuesInt.AES_CBC_MAC_128_64:
                cbitKey = 128;
                cbitTag = 64;
                break;

            case AlgorithmValuesInt.AES_CBC_MAC_256_64:
                cbitKey = 256;
                cbitTag = 64;
                break;

            case AlgorithmValuesInt.AES_CBC_MAC_128_128:
                cbitKey = 128;
                cbitTag = 128;
                break;

            case AlgorithmValuesInt.AES_CBC_MAC_256_128:
                cbitKey = 256;
                cbitTag = 128;
                break;

            default:
                throw new Exception("Unrecognized algorithm");
            }

            IMac mac = new CbcBlockCipherMac(aes, cbitTag, null);

            if (K.Length != cbitKey / 8)
            {
                throw new CoseException("Key is incorrectly sized");
            }
            ContentKey = new KeyParameter(K);

            //  Build the text to be digested

            mac.Init(ContentKey);

            byte[] toDigest = BuildContentBytes();

            byte[] C = new byte[128 / 8];
            mac.BlockUpdate(toDigest, 0, toDigest.Length);
            mac.DoFinal(C, 0);

            byte[] rgbResult = new byte[cbitTag / 8];
            Array.Copy(C, 0, rgbResult, 0, cbitTag / 8);

            return(rgbResult);
        }
        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));
        }
Beispiel #9
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));
        }
Beispiel #10
0
        public virtual ITestResult Perform()
        {
            KeyParameter key    = new KeyParameter(keyBytes);
            IBlockCipher cipher = new DesEngine();
            IMac         mac    = new CbcBlockCipherMac(cipher);

            //
            // standard DAC - zero IV
            //
            mac.Init(key);

            mac.BlockUpdate(input1, 0, input1.Length);

            byte[] outBytes = new byte[4];

            mac.DoFinal(outBytes, 0);

            if (!Arrays.AreEqual(outBytes, output1))
            {
                return(new SimpleTestResult(false, Name + ": Failed - expected "
                                            + Hex.ToHexString(output1) + " got " + Hex.ToHexString(outBytes)));
            }

            //
            // mac with IV.
            //
            ParametersWithIV param = new ParametersWithIV(key, ivBytes);

            mac.Init(param);

            mac.BlockUpdate(input1, 0, input1.Length);

            outBytes = new byte[4];

            mac.DoFinal(outBytes, 0);

            if (!Arrays.AreEqual(outBytes, output2))
            {
                return(new SimpleTestResult(false, Name + ": Failed - expected "
                                            + Hex.ToHexString(output2) + " got " + Hex.ToHexString(outBytes)));
            }

            //
            // CFB mac with IV - 8 bit CFB mode
            //
            param = new ParametersWithIV(key, ivBytes);

            mac = new CfbBlockCipherMac(cipher);

            mac.Init(param);

            mac.BlockUpdate(input1, 0, input1.Length);

            outBytes = new byte[4];

            mac.DoFinal(outBytes, 0);

            if (!Arrays.AreEqual(outBytes, output3))
            {
                return(new SimpleTestResult(false, Name + ": Failed - expected "
                                            + Hex.ToHexString(output3) + " got " + Hex.ToHexString(outBytes)));
            }

            //
            // word aligned data - zero IV
            //
            mac.Init(key);

            mac.BlockUpdate(input2, 0, input2.Length);

            outBytes = new byte[4];

            mac.DoFinal(outBytes, 0);

            if (!Arrays.AreEqual(outBytes, output4))
            {
                return(new SimpleTestResult(false, Name + ": Failed - expected "
                                            + Hex.ToHexString(output4) + " got " + Hex.ToHexString(outBytes)));
            }

            //
            // word aligned data - zero IV - CBC padding
            //
            mac = new CbcBlockCipherMac(cipher, new Pkcs7Padding());

            mac.Init(key);

            mac.BlockUpdate(input2, 0, input2.Length);

            outBytes = new byte[4];

            mac.DoFinal(outBytes, 0);

            if (!Arrays.AreEqual(outBytes, output5))
            {
                return(new SimpleTestResult(false, Name + ": Failed - expected "
                                            + Hex.ToHexString(output5) + " got " + Hex.ToHexString(outBytes)));
            }

            //
            // non-word aligned data - zero IV - CBC padding
            //
            mac.Reset();

            mac.BlockUpdate(input1, 0, input1.Length);

            outBytes = new byte[4];

            mac.DoFinal(outBytes, 0);

            if (!Arrays.AreEqual(outBytes, output6))
            {
                return(new SimpleTestResult(false, Name + ": Failed - expected "
                                            + Hex.ToHexString(output6) + " got " + Hex.ToHexString(outBytes)));
            }

            //
            // non-word aligned data - zero IV - CBC padding
            //
            mac.Init(key);

            mac.BlockUpdate(input1, 0, input1.Length);

            outBytes = new byte[4];

            mac.DoFinal(outBytes, 0);

            if (!Arrays.AreEqual(outBytes, output6))
            {
                return(new SimpleTestResult(false, Name + ": Failed - expected "
                                            + Hex.ToHexString(output6) + " got " + Hex.ToHexString(outBytes)));
            }

            return(new SimpleTestResult(true, Name + ": Okay"));
        }
        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));
        }
Beispiel #12
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));
        }