public Scp03CryptoSession() { ecb_cipher = new BufferedBlockCipher(new AesEngine()); cbc_cipher = new BufferedBlockCipher(new CbcBlockCipher(new AesEngine())); cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(new AesEngine()), new ISO7816d4Padding()); cmac = new CMac(new AesEngine()); }
private static byte[] Scp03_kdf(byte[] key, byte constant, byte[] context, int blocklen_bits) { // 11 bytes byte[] label = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //TODO: test, should order be reversed? byte[] bo = Arrays.ConcatenateAll( label, // 11 bytes of label new byte[] { constant }, // constant for the last byte new byte[] { 0x00 }, // separator new byte[] { (byte)((blocklen_bits >> 8) & 0xFF) }, // block size in two bytes new byte[] { (byte)(blocklen_bits & 0xFF) } ); byte[] blocka = bo; byte[] blockb = context; IBlockCipher cipher = new AesEngine(); CMac cmac = new CMac(cipher); KDFCounterBytesGenerator kdf = new KDFCounterBytesGenerator(cmac); kdf.Init(new KDFCounterParameters(key, blocka, blockb, 8)); // counter size in bits byte[] cgram = new byte[blocklen_bits / 8]; kdf.GenerateBytes(cgram, 0, cgram.Length); return(cgram); }
public byte[] Generate(byte[] agreed) { IMac prfMac; if (prfAlgorithm == FipsPrfAlgorithm.AesCMac) { Internal.IBlockCipher aesEng = FipsAes.ENGINE_PROVIDER.CreateEngine(EngineUsage.GENERAL); aesEng.Init(true, new KeyParameter(salt ?? new byte[16])); prfMac = new CMac(aesEng); prfMac.Init(null); } else { prfMac = FipsShs.CreateHmac((DigestAlgorithm)prfAlgorithm.BaseAlgorithm); prfMac.Init(new KeyParameter(salt ?? new byte[((HMac)prfMac).GetUnderlyingDigest().GetByteLength()])); } byte[] mac = Macs.DoFinal(prfMac, agreed, 0, agreed.Length); // ZEROIZE Arrays.Fill(agreed, (byte)0); return(mac); }
/// <summary> /// Generate digest. The digest can be reused. /// </summary> /// <param name="parameters">Parameters.</param> /// <returns></returns> /// <exception cref="Exception"/> public IMac GenerateDigest(ICipherParameters parameters) { IMac digest = new CMac(this.BlockAlgorithm.GenerateEngine(), this.HashSize); digest.Init(parameters); return(digest); }
public IMac CreateEngine(EngineUsage usage) { IMac mac = new CMac(baseProvider.CreateEngine(EngineUsage.ENCRYPTION), macSizeInBits); mac.Init(null); return(mac); }
public static void Main(string[] args) { if (args.Length != 2) { Console.WriteLine("Usage: ECCTest.exe ecdsaca.der ecdsakey.cng"); return; } X509Certificate2 cert = new X509Certificate2(args[0]); PublicKey publicKey = cert.PublicKey; ECDsaCng ecdsaPublicKey = GetECDSAFromPublicKey(publicKey); ecdsaPublicKey.HashAlgorithm = CngAlgorithm.Sha512; byte[] cngBlob = File.ReadAllBytes(args[1]); CngKey cngKey = CngKey.Import(cngBlob, CngKeyBlobFormat.GenericPrivateBlob, CngProvider.MicrosoftSoftwareKeyStorageProvider); ECDsaCng ecdsaPrivateKey = new ECDsaCng(cngKey); ecdsaPrivateKey.HashAlgorithm = CngAlgorithm.Sha512; byte[] data = new byte[256]; for (int i = 0; i < data.Length; i++) { data[i] = (byte)i; } byte[] signature = ecdsaPrivateKey.SignData(data); PrintBytes("signature", signature); Console.WriteLine("Signature verified: " + ecdsaPublicKey.VerifyData(data, signature)); ECDiffieHellmanBc alice = new ECDiffieHellmanBc(); ECDiffieHellmanCng bob = new ECDiffieHellmanCng(); byte[] aliceKey = alice.DeriveKeyMaterial(bob.PublicKey); byte[] bobKey = bob.DeriveKeyMaterial(alice.PublicKey); PrintBytes("alice key", aliceKey); PrintBytes("bob key", bobKey); Console.WriteLine("Running CMAC test"); byte[] keyBytes = new byte[24]; KeyParameter key = new KeyParameter(keyBytes); byte[] hashedData = new byte[31]; for (int i = 0; i < hashedData.Length; i++) { hashedData[i] = (byte)i; } CMac cmac = new CMac(new AesEngine(), 128); cmac.Init(key); cmac.BlockUpdate(hashedData, 0, hashedData.Length); byte[] hash = new byte[cmac.GetMacSize()]; cmac.DoFinal(hash, 0); PrintBytes("hash", hash); }
protected byte[] AES_CMAC(CBORObject alg, byte[] K) { int cbitKey; int cbitTag; IBlockCipher aes = new AesFastEngine(); CMac mac = new CMac(aes); 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.TextString); switch (alg.AsString()) { case "AES-CMAC-128/64": cbitKey = 128; cbitTag = 64; break; case "AES-CMAC-256/64": cbitKey = 256; cbitTag = 64; break; default: throw new Exception("Unrecognized algorithm"); } 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[] rgbOut = new byte[cbitTag / 8]; Array.Copy(C, 0, rgbOut, 0, cbitTag / 8); return(rgbOut); }
private static byte[] Scp03_mac(byte[] keybytes, byte[] msg, int lengthBits) { // FIXME: programmatically set the crypto backend IBlockCipher cipher = new AesEngine(); CMac cmac = new CMac(cipher); cmac.Init(new KeyParameter(keybytes)); cmac.BlockUpdate(msg, 0, msg.Length); byte[] outVal = new byte[cmac.GetMacSize()]; cmac.DoFinal(outVal, 0); return(Arrays.CopyOf(outVal, lengthBits / 8)); }
private byte[] AesCMac(byte[] inputBytes) { var mac = new CMac(_myAes); var keyParam = new KeyParameter(_config.DigestKey); mac.Init(keyParam); mac.BlockUpdate(inputBytes, 0, inputBytes.Length); var hash = new byte[mac.GetMacSize()]; mac.DoFinal(hash, 0); return(hash); }
private void TestExceptions() { try { CMac mac = new CMac(new AesEngine()); mac.Init(new ParametersWithIV(new KeyParameter(new byte[16]), new byte[16])); Fail("CMac does not accept IV"); } catch (ArgumentException) { // Expected } }
public bool HasTestPassed(EngineProvider provider) { byte[] input16 = Hex.Decode("6bc1bee22e409f96e93d7e117393172a"); byte[] output_k128_m16 = Hex.Decode("c0b9bbee139722ab"); IMac mac = new CMac(provider.CreateEngine(EngineUsage.GENERAL), 64); //128 bytes key KeyParameter key = new KeyParameter(Hex.Decode("0102020404070708080b0b0d0d0e0e101013131515161619")); byte[] output = Macs.DoFinal(mac, key, input16, 0, input16.Length); return(Arrays.AreEqual(FipsKats.Values[FipsKats.Vec.TripleDesCMacTag], output)); }
public bool HasTestPassed(EngineProvider provider) { byte[] keyBytes128 = Hex.Decode("2b7e151628aed2a6abf7158809cf4f3c"); byte[] input16 = Hex.Decode("6bc1bee22e409f96e93d7e117393172a"); byte[] output_k128_m16 = Hex.Decode("070a16b46b4d4144f79bdd9dd04a287c"); IMac mac = new CMac(provider.CreateEngine(EngineUsage.GENERAL), 128); //128 bits key KeyParameter key = new KeyParameter(keyBytes128); byte[] output = Macs.DoFinal(mac, key, input16, 0, input16.Length); return(Arrays.AreEqual(FipsKats.Values[FipsKats.Vec.AesCMacTag], output)); }
public static byte[] GetCMACDigest(byte[] data, byte[] key) { IBlockCipher cipher = new AesEngine(); IMac mac = new CMac(cipher, 128); KeyParameter keyParam = new KeyParameter(key); mac.Init(keyParam); mac.BlockUpdate(data, 0, data.Length); byte[] outBytes = new byte[16]; mac.DoFinal(outBytes, 0); return(outBytes); }
/// <summary> /// Creates a CMAC primitive using a symmetric block cipher primitive configured with default block size. /// Default block sizes (and so, output sizes) can be found by querying <see cref="Athena" />. /// </summary> /// <param name="cipherEnum"> /// Cipher primitive to use as the basis for the CMAC construction. Block size must be 64 or 128 /// bits. /// </param> /// <param name="key">Cryptographic key to use in the MAC operation.</param> /// <param name="salt">Cryptographic salt to use in the MAC operation, if any.</param> /// <returns>Pre-initialised CMAC primitive as a <see cref="IMac" />.</returns> public static IMac CreateCmacPrimitive(BlockCipher cipherEnum, byte[] key, byte[] salt = null) { int?defaultBlockSize = Athena.Cryptography.BlockCiphers[cipherEnum].DefaultBlockSizeBits; if (defaultBlockSize != 64 && defaultBlockSize != 128) { throw new NotSupportedException("CMAC/OMAC1 only supports ciphers with 64 / 128 bit block sizes."); } var macObj = new CMac(CipherFactory.CreateBlockCipher(cipherEnum, null)); macObj.Init(key); if (salt.IsNullOrZeroLength() == false) { macObj.BlockUpdate(salt, 0, salt.Length); } return(macObj); }
public void runTest() { IBlockCipher cipher = new AesEngine(); IMac mac = new CMac(cipher, 128); KeyParameter key = new KeyParameter(keyBytes128); var Encryption_derived_Block_AES128 = new KeyDerevationBlock() { Counter = "01", KeyUsageIndicator = "0000", Seperator = "00", AlgorithmIndicator = "0002", Length = "0080" }; byte[] EncryptionBlockInput = Encryption_derived_Block_AES128.CreateKeyDevBytes(); var MAC_derived_Block_AES128 = new KeyDerevationBlock() { Counter = "01", KeyUsageIndicator = "0001", Seperator = "00", AlgorithmIndicator = "0002", Length = "0080" }; byte[] MACnBlockInput = MAC_derived_Block_AES128.CreateKeyDevBytes(); Console.WriteLine("----------------------------------------------------------------"); Console.WriteLine("Deriving a Key for Encryption"); Console.WriteLine("Using input derivation key: " + Encryption_derived_Block_AES128.CreateKeyDev()); mac.Init(key); mac.BlockUpdate(EncryptionBlockInput, 0, EncryptionBlockInput.Length); byte[] outBytes = new byte[16]; mac.DoFinal(outBytes, 0); Console.WriteLine("Derived Encryption Key:" + Hex.ToHexString(outBytes)); Derived_Encryption_Key = outBytes; Console.WriteLine("----------------------------------------------------------------"); Console.WriteLine("Deriving a Key for MAC"); Console.WriteLine("Using input derivation key: " + MAC_derived_Block_AES128.CreateKeyDev()); mac.Init(key); mac.BlockUpdate(MACnBlockInput, 0, MACnBlockInput.Length); mac.DoFinal(outBytes, 0); Console.WriteLine("Derived MAC Key:" + Hex.ToHexString(outBytes)); Derived_MAC_Key = outBytes; Console.WriteLine("----------------------------------------------------------------"); //build block }
public static void PerformTest() { //ISO20038 vp = new ISO20038(); //vp.runTest(); //** Console.WriteLine(" +-----+ +-----+ +-----+ +-----+ +-----+ +---+----+ "); Console.WriteLine(" | M_1 | | M_2 | | M_n | | M_1 | | M_2 | |M_n|10^i| "); Console.WriteLine(" +-----+ +-----+ +-----+ +-----+ +-----+ +---+----+ "); Console.WriteLine(" | | | +--+ | | | +--+ "); Console.WriteLine(" | +--->(+) +--->(+)<-|K1| | +--->(+) +--->(+)<-|K2| "); Console.WriteLine(" | | | | | +--+ | | | | | +--+ "); Console.WriteLine(" +-----+ | +-----+ | +-----+ +-----+ | +-----+ | +-----+ "); Console.WriteLine(" |AES_K| | |AES_K| | |AES_K| |AES_K| | |AES_K | | |AES_K| "); Console.WriteLine(" +-----+ | +-----+ | +-----+ +-----+ | +-----+ | +-----+ "); Console.WriteLine(" | | | | | | | | | | "); Console.WriteLine(" +-----+ +-----+ | +-----+ +-----+ | "); Console.WriteLine(" | | "); Console.WriteLine(" +-----+ +-----+ "); Console.WriteLine(" | T | | T | "); Console.WriteLine(" +-----+ +-----+ "); IBlockCipher cipher = new AesEngine(); IMac mac = new CMac(cipher, 128); Console.WriteLine("CMAC Init.. Cipher: " + cipher.AlgorithmName); Console.WriteLine("CMAC Init.. MAC BlockSize: " + 128); Console.WriteLine("----------------------------------------------------------------"); //128 bytes key KeyParameter key = new KeyParameter(keyBytes128); Console.WriteLine("Example 1:Message len = 0 bytes, key = " + keyBytes128.Length + " bytes"); Console.WriteLine("M: <empty string>"); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes128)); // 0 bytes message - 128 bytes key mac.Init(key); mac.BlockUpdate(input0, 0, input0.Length); byte[] outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k128_m0)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k128_m0) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 16 bytes message - 128 bytes key Console.WriteLine("Example 2: Message len = " + input16.Length + " bytes, key = " + keyBytes128.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input16)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes128)); mac.Init(key); mac.BlockUpdate(input16, 0, input16.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k128_m16)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k128_m16) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 40 bytes message - 128 bytes key Console.WriteLine("Example 3: Message len = " + input40.Length + " bytes, key = " + keyBytes128.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input40)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes128)); mac.Init(key); mac.BlockUpdate(input40, 0, input40.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k128_m40)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k128_m40) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 64 bytes message - 128 bytes key Console.WriteLine("Example 4: Message len = " + input64.Length + " bytes, key = " + keyBytes128.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input64)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes128)); mac.Init(key); mac.BlockUpdate(input64, 0, input64.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if ((!AreEqual(outBytes, output_k128_m64))) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k128_m64) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); //192 bytes key key = new KeyParameter(keyBytes192); Console.WriteLine("Example 5: Message len = 0 bytes, key = " + keyBytes192.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input0)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes192)); // 0 bytes message - 192 bytes ke mac.Init(key); mac.BlockUpdate(input0, 0, input0.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k192_m0)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k192_m0) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("Generated CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 16 bytes message - 192 bytes key Console.WriteLine("Example 6: Message len = " + input16.Length + " bytes, key = " + keyBytes192.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input16)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes192)); mac.Init(key); mac.BlockUpdate(input16, 0, input16.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k192_m16)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k192_m16) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("Generated CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 40 bytes message - 192 bytes key Console.WriteLine("Example 7: Message len = " + input40.Length + " bytes, key = " + keyBytes192.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input40)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes192)); mac.Init(key); mac.BlockUpdate(input40, 0, input40.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k192_m40)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k192_m40) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("Generated CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 64 bytes message - 192 bytes key Console.WriteLine("Example 8: Message len = " + input64.Length + " bytes, key = " + keyBytes192.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input64)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes192)); mac.Init(key); mac.BlockUpdate(input64, 0, input64.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k192_m64)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k192_m64) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("Generated CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); //256 bytes key key = new KeyParameter(keyBytes256); Console.WriteLine("Example 9: Message len = 0 bytes, key = " + keyBytes256.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input0)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes256)); // 0 bytes message - 256 bytes key mac.Init(key); mac.BlockUpdate(input0, 0, input0.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k256_m0)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k256_m0) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("Generated CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 16 bytes message - 256 bytes key Console.WriteLine("Example 10: Message len = " + input16.Length + " bytes, key = " + keyBytes256.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input16)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes256)); mac.Init(key); mac.BlockUpdate(input16, 0, input16.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k256_m16)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k256_m16) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("Generated CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 40 bytes message - 256 bytes key Console.WriteLine("Example 11: Message len = " + input40.Length + " bytes, key = " + keyBytes256.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input40)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes256)); mac.Init(key); mac.BlockUpdate(input40, 0, input40.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k256_m40)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k256_m40) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("Generated CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); // 64 bytes message - 256 bytes key Console.WriteLine("Example 12: Message len = " + input64.Length + " bytes, key = " + keyBytes256.Length + " bytes"); Console.WriteLine("M: " + Hex.ToHexString(input64)); Console.WriteLine("KEY: " + Hex.ToHexString(keyBytes256)); mac.Init(key); mac.BlockUpdate(input64, 0, input64.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k256_m64)) { Console.WriteLine("Failed - expected " + Hex.ToHexString(output_k256_m64) + " got " + Hex.ToHexString(outBytes)); } Console.WriteLine("Generated CMAC:" + Hex.ToHexString(outBytes)); Console.WriteLine("----------------------------------------------------------------"); TestExceptions(); }
public override void PerformTest() { IBlockCipher cipher = new AesEngine(); IMac mac = new CMac(cipher, 128); //128 bytes key KeyParameter key = new KeyParameter(keyBytes128); // 0 bytes message - 128 bytes key mac.Init(key); mac.BlockUpdate(input0, 0, input0.Length); byte[] outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k128_m0)) { Fail("Failed - expected " + Hex.ToHexString(output_k128_m0) + " got " + Hex.ToHexString(outBytes)); } // 16 bytes message - 128 bytes key mac.Init(key); mac.BlockUpdate(input16, 0, input16.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k128_m16)) { Fail("Failed - expected " + Hex.ToHexString(output_k128_m16) + " got " + Hex.ToHexString(outBytes)); } // 40 bytes message - 128 bytes key mac.Init(key); mac.BlockUpdate(input40, 0, input40.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k128_m40)) { Fail("Failed - expected " + Hex.ToHexString(output_k128_m40) + " got " + Hex.ToHexString(outBytes)); } // 64 bytes message - 128 bytes key mac.Init(key); mac.BlockUpdate(input64, 0, input64.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k128_m64)) { Fail("Failed - expected " + Hex.ToHexString(output_k128_m64) + " got " + Hex.ToHexString(outBytes)); } //192 bytes key key = new KeyParameter(keyBytes192); // 0 bytes message - 192 bytes key mac.Init(key); mac.BlockUpdate(input0, 0, input0.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k192_m0)) { Fail("Failed - expected " + Hex.ToHexString(output_k192_m0) + " got " + Hex.ToHexString(outBytes)); } // 16 bytes message - 192 bytes key mac.Init(key); mac.BlockUpdate(input16, 0, input16.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k192_m16)) { Fail("Failed - expected " + Hex.ToHexString(output_k192_m16) + " got " + Hex.ToHexString(outBytes)); } // 40 bytes message - 192 bytes key mac.Init(key); mac.BlockUpdate(input40, 0, input40.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k192_m40)) { Fail("Failed - expected " + Hex.ToHexString(output_k192_m40) + " got " + Hex.ToHexString(outBytes)); } // 64 bytes message - 192 bytes key mac.Init(key); mac.BlockUpdate(input64, 0, input64.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k192_m64)) { Fail("Failed - expected " + Hex.ToHexString(output_k192_m64) + " got " + Hex.ToHexString(outBytes)); } //256 bytes key key = new KeyParameter(keyBytes256); // 0 bytes message - 256 bytes key mac.Init(key); mac.BlockUpdate(input0, 0, input0.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k256_m0)) { Fail("Failed - expected " + Hex.ToHexString(output_k256_m0) + " got " + Hex.ToHexString(outBytes)); } // 16 bytes message - 256 bytes key mac.Init(key); mac.BlockUpdate(input16, 0, input16.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k256_m16)) { Fail("Failed - expected " + Hex.ToHexString(output_k256_m16) + " got " + Hex.ToHexString(outBytes)); } // 40 bytes message - 256 bytes key mac.Init(key); mac.BlockUpdate(input40, 0, input40.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k256_m40)) { Fail("Failed - expected " + Hex.ToHexString(output_k256_m40) + " got " + Hex.ToHexString(outBytes)); } // 64 bytes message - 256 bytes key mac.Init(key); mac.BlockUpdate(input64, 0, input64.Length); outBytes = new byte[16]; mac.DoFinal(outBytes, 0); if (!AreEqual(outBytes, output_k256_m64)) { Fail("Failed - expected " + Hex.ToHexString(output_k256_m64) + " got " + Hex.ToHexString(outBytes)); } TestExceptions(); }
public string calculate(string plainText, string key, string algorithm, int macSize) { if (!isValidAlgorithm(algorithm)) { this.error.setError("CM001", "Invalid Symmetric block algorithm for CMAC"); return(""); } SymmetricBlockAlgorithm symmetricBlockAlgorithm = SymmetricBlockAlgorithmUtils.getSymmetricBlockAlgorithm(algorithm, this.error); SymmetricBlockCipher symCipher = new SymmetricBlockCipher(); IBlockCipher blockCipher = symCipher.getCipherEngine(symmetricBlockAlgorithm); if (symCipher.HasError()) { this.error = symCipher.GetError(); return(""); } if (macSize > blockCipher.GetBlockSize() * 8) { this.error.setError("CM002", "The mac length must be less or equal than the algorithm block size."); return(""); } byte[] byteKey = SecurityUtils.GetHexa(key, "CM003", this.error); if (this.HasError()) { return(""); } EncodingUtil eu = new EncodingUtil(); byte[] byteInput = eu.getBytes(plainText); ICipherParameters parms = new KeyParameter(byteKey); CMac mac = null; if (macSize != 0) { mac = new CMac(blockCipher, macSize); } else { mac = new CMac(blockCipher); } try { mac.Init(parms); }catch (Exception e) { this.error.setError("CM004", e.Message); return(""); } byte[] resBytes = new byte[mac.GetMacSize()]; mac.BlockUpdate(byteInput, 0, byteInput.Length); mac.DoFinal(resBytes, 0); string result = toHexastring(resBytes); if (!this.error.existsError()) { return(result); } return(""); }