private void FromDescriptionTest() { MacDescription mds = new MacDescription(64, Digests.SHA256); MacDescription mds2 = new MacDescription(mds.ToBytes()); MacDescription mds3 = new MacDescription(mds2.ToStream()); if (!mds.Equals(mds2) || !mds.Equals(mds3)) { throw new Exception("MacStreamTest: Description serialization has failed!"); } mds = new MacDescription(32, BlockCiphers.Rijndael, IVSizes.V128, BlockSizes.B128, RoundCounts.R14); mds2 = new MacDescription(mds.ToBytes()); mds3 = new MacDescription(mds2.ToStream()); if (!mds.Equals(mds2) || !mds.Equals(mds3)) { throw new Exception("MacStreamTest: Description serialization has failed!"); } mds = new MacDescription(32, Digests.Blake2S256); mds2 = new MacDescription(mds.ToBytes()); mds3 = new MacDescription(mds2.ToStream()); if (!mds.Equals(mds2) || !mds.Equals(mds3)) { throw new Exception("MacStreamTest: Description serialization has failed!"); } }
/// <summary> /// Initialize the MacKey structure using a Stream /// </summary> /// /// <param name="KeyStream">The Stream containing the MacKey</param> public MacKey(Stream KeyStream) { BinaryReader reader = new BinaryReader(KeyStream); Description = new MacDescription(reader.ReadBytes(MacDescription.GetHeaderSize())); KeyId = reader.ReadBytes(KEYUID_SIZE); }
/// <summary> /// Initialize the class with a MacDescription structure and a Key /// </summary> /// /// <param name="Description">A MacDescription structure containing details about the Mac generator</param> /// <param name="MacKey">A KeyParams containing the Mac key and Iv; note the Ikm parameter in KeyParams is not used</param> /// <param name="DisposeEngine">Dispose of digest engine when <see cref="Dispose()"/> on this class is called; default is false</param> /// /// <exception cref="CryptoProcessingException">Thrown if the Mac key or parameters are invalid</exception> public MacStream(MacDescription Description, KeyParams MacKey, bool DisposeEngine = false) { try { _macEngine = MacFromDescription.GetInstance(Description); _macEngine.Initialize(MacKey.Key, MacKey.IV); } catch (Exception ex) { throw new CryptoProcessingException("MacStream:CTor", "The Mac parameters or key is invalid!", ex); } m_blockSize = _macEngine.BlockSize; m_disposeEngine = DisposeEngine; }
/// <summary> /// MacKey structure constructor. /// <para>KeyID and ExtRandom values must each be 16 bytes in length. /// If they are not specified they will be populated automatically.</para> /// </summary> /// /// <param name="Description">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Common.MacDescription">MacDescription</see> structure containing a complete description of the cipher instance</param> /// <param name="KeyId">The unique 16 byte ID field used to identify this key. A null value auto generates this field</param> /// /// <exception cref="CryptoProcessingException">Thrown if either the KeyId or ExtensionKey fields are null or invalid</exception> public MacKey(MacDescription Description, byte[] KeyId = null) { this.Description = Description; if (KeyId == null) { this.KeyId = Guid.NewGuid().ToByteArray(); } else if (KeyId.Length != KEYUID_SIZE) { throw new CryptoProcessingException("MacKey:CTor", "The KeyId must be exactly 16 bytes!", new ArgumentOutOfRangeException()); } else { this.KeyId = KeyId; } }
/// <summary> /// Get an uninitialized Mac generator from its description structure /// </summary> /// /// <param name="Description">The structure describing the Mac generator</param> /// /// <returns>An initialized Mac generator</returns> /// /// <exception cref="CryptoProcessingException">Thrown if the Mac type is not supported</exception> public static IMac GetInstance(MacDescription Description) { switch ((Macs)Description.MacType) { case Macs.CMAC: { return(new CMAC((BlockCiphers)Description.EngineType)); } case Macs.HMAC: { return(new HMAC((Digests)Description.HmacEngine)); } default: throw new CryptoProcessingException("MacFromDescription:GetInstance", "The Mac generator is not recognized!"); } }
private void HmacDescriptionTest() { CSPPrng rng = new CSPPrng(); byte[] data = rng.GetBytes(rng.Next(100, 400)); byte[] key = rng.GetBytes(64); HMAC mac = new HMAC(Digests.SHA256); mac.Initialize(key); byte[] c1 = mac.ComputeMac(data); MacDescription mds = new MacDescription(64, Digests.SHA256); MacStream mst = new MacStream(mds, new KeyParams(key)); mst.Initialize(new MemoryStream(data)); byte[] c2 = mst.ComputeMac(); if (!Evaluate.AreEqual(c1, c2)) { throw new Exception("MacStreamTest: HMAC code arrays are not equal!"); } }
private void CmacDescriptionTest() { CSPPrng rng = new CSPPrng(); byte[] data = rng.GetBytes(rng.Next(100, 400)); byte[] key = rng.GetBytes(32); byte[] iv = rng.GetBytes(16); CMAC mac = new CMAC(BlockCiphers.Rijndael); mac.Initialize(key, iv); byte[] c1 = mac.ComputeMac(data); MacDescription mds = new MacDescription(32, BlockCiphers.Rijndael, IVSizes.V128, BlockSizes.B128, RoundCounts.R14); MacStream mst = new MacStream(mds, new KeyParams(key, iv)); mst.Initialize(new MemoryStream(data)); byte[] c2 = mst.ComputeMac(); if (!Evaluate.AreEqual(c1, c2)) { throw new Exception("MacStreamTest: CMAC code arrays are not equal!"); } }
/// <summary> /// Set the MacDescription structure /// </summary> /// /// <param name="KeyStream">The stream containing a key package</param> /// <param name="Description">The MacDescription structure</param> public static void SetCipherDescription(Stream KeyStream, MacDescription Description) { KeyStream.Seek(MACDSC_SEEK, SeekOrigin.Begin); new BinaryWriter(KeyStream).Write(Description.ToBytes()); }