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); }
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); }