Exemplo n.º 1
0
 /// <summary>
 /// Applies full triple DES MAC as defined in [ISO 9797-1] as MAC Algorithm 1 with output transformation 1,
 /// without truncation, and withtriple DES taking the place of the block cipher.
 /// See Global Platform 2.1.1 Card Spec Section B.1.2.1
 /// </summary>
 /// <param name="key">3DES key</param>
 /// <param name="iv">Initial Vector</param>
 /// <param name="data">Data to MAC</param>
 /// <returns>Full triple DES MAC</returns>
 public static byte[] FullTripleDESMAC(Key key, byte[] iv, byte[] data)
 {
     byte[] enc    = TripleDESCBC(new Key(key.BuildTripleDesKey()), iv, data, MODE_ENCRYPT);
     byte[] result = new byte[8];
     Array.Copy(enc, enc.Length - 8, result, 0, 8);
     return(result);
 }
Exemplo n.º 2
0
 /// <summary>
 /// Applies full triple DES MAC as defined in [ISO 9797-1] as MAC Algorithm 1 with output transformation 1, 
 /// without truncation, and withtriple DES taking the place of the block cipher. 
 /// See Global Platform 2.1.1 Card Spec Section B.1.2.1
 /// </summary>
 /// <param name="key">3DES key</param>
 /// <param name="iv">Initial Vector</param>
 /// <param name="data">Data to MAC</param>
 /// <returns>Full triple DES MAC</returns>
 public static byte[] FullTripleDESMAC(Key key, byte[] iv, byte[] data)
 {
     byte[] enc = TripleDESCBC(new Key(key.BuildTripleDesKey()), iv, data, MODE_ENCRYPT);
     byte[] result = new byte[8];
     Array.Copy(enc, enc.Length - 8, result, 0, 8);
     return result;
 }
        private byte[] EncodeKeyData(Key key, Key kek, bool addKCV, int keyFormat)
        {
            MemoryStream keyData = new MemoryStream();

            if (keyFormat == KEY_FORMAT_1)
            {
                // Key encryption algorithm
                keyData.WriteByte(CryptoUtil.ALG_DES);

                // Encrypted key data length
                keyData.WriteByte(0x10);

                byte[] encryptedKey = CryptoUtil.TripleDESECB(kek, key.Value, CryptoUtil.MODE_ENCRYPT);
                keyData.Write(encryptedKey, 0, encryptedKey.Length);

                if (addKCV)
                {
                    // KCV length
                    keyData.WriteByte(0x03);

                    // Calculate KCV
                    byte[] kcv = CryptoUtil.TripleDESECB(new Key(key.BuildTripleDesKey()),
                                                         CryptoUtil.BINARY_ZEROS_8_BYTE_BLOCK,
                                                         CryptoUtil.MODE_ENCRYPT);
                    keyData.Write(kcv, 0, 3);
                }
                else
                {
                    keyData.WriteByte(0x00);
                }
            }

            return(keyData.ToArray());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Applies Retail MAC as defined in [ISO 9797-1] as MAC Algorithm 1 with output
        /// transformation 3, without truncation, and withDES taking the place of the block cipher.
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="iv">Initial Vector</param>
        /// <param name="data">Data to MAC</param>
        /// <returns>Retial MAC</returns>

        public static byte[] SingleDESFullTripleDESMAC(Key key, byte[] iv, byte[] data)
        {
            byte[] intermeidateResult;
            byte[] result = new byte[8];
            if (data.Length > 8)
            {
                intermeidateResult = DESCBC(new Key(key.BuildDesKey()), iv, SubArray(data, 0, data.Length - 8), MODE_ENCRYPT);
                Array.Copy(intermeidateResult, intermeidateResult.Length - 8, result, 0, 8);
                intermeidateResult = TripleDESCBC(new Key(key.BuildTripleDesKey()), result, SubArray(data, data.Length - 8, 8), MODE_ENCRYPT);
            }
            else
            {
                intermeidateResult = TripleDESCBC(new Key(key.BuildTripleDesKey()), iv, SubArray(data, data.Length - 8, 8), MODE_ENCRYPT);
            }
            Array.Copy(intermeidateResult, intermeidateResult.Length - 8, result, 0, 8);
            return(result);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Applies Retail MAC as defined in [ISO 9797-1] as MAC Algorithm 1 with output 
        /// transformation 3, without truncation, and withDES taking the place of the block cipher. 
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="iv">Initial Vector</param>
        /// <param name="data">Data to MAC</param>
        /// <returns>Retial MAC</returns>

        public static byte[] SingleDESFullTripleDESMAC(Key key, byte[] iv, byte[] data)
        {
            byte[] intermeidateResult;
            byte[] result = new byte[8];
            if (data.Length > 8)
            {
                intermeidateResult = DESCBC(new Key(key.BuildDesKey()), iv, SubArray(data, 0, data.Length - 8), MODE_ENCRYPT);
                Array.Copy(intermeidateResult, intermeidateResult.Length - 8, result, 0, 8);
                intermeidateResult = TripleDESCBC(new Key(key.BuildTripleDesKey()), result, SubArray(data, data.Length - 8, 8), MODE_ENCRYPT);
            }
            else
            {
                intermeidateResult = TripleDESCBC(new Key(key.BuildTripleDesKey()), iv, SubArray(data, data.Length - 8, 8), MODE_ENCRYPT);
            }
            Array.Copy(intermeidateResult, intermeidateResult.Length - 8, result, 0, 8);
            return result;
        }
Exemplo n.º 6
0
        private byte[] EncodeKeyData(Key key, Key kek, bool addKCV, int keyFormat)
        {
            MemoryStream keyData = new MemoryStream();
            if (keyFormat == KEY_FORMAT_1)
            {
                // Key encryption algorithm
                keyData.WriteByte(CryptoUtil.ALG_DES);

                // Encrypted key data length
                keyData.WriteByte(0x10);

                byte[] encryptedKey = CryptoUtil.TripleDESECB(kek, key.Value, CryptoUtil.MODE_ENCRYPT);
                keyData.Write(encryptedKey, 0, encryptedKey.Length);

                if (addKCV)
                {

                    // KCV length
                    keyData.WriteByte(0x03);

                    // Calculate KCV
                    byte[] kcv = CryptoUtil.TripleDESECB(new Key(key.BuildTripleDesKey()),
                        CryptoUtil.BINARY_ZEROS_8_BYTE_BLOCK,
                        CryptoUtil.MODE_ENCRYPT);
                    keyData.Write(kcv, 0, 3);
                }
                else
                {
                    keyData.WriteByte(0x00);
                }
            }

            return keyData.ToArray();
        }