/// <remarks> /// Writes a memorystream to the key package file /// </remarks> private void WriteKeyStream(MemoryStream InputStream) { InputStream.Seek(0, SeekOrigin.Begin); try { BinaryWriter keyWriter = new BinaryWriter(m_keyStream); BinaryReader keyReader = new BinaryReader(InputStream); // policy flag is not encrypted long policies = keyReader.ReadInt64(); keyWriter.Write(policies); // get the header and keying material byte[] data = new byte[InputStream.Length - PackageKey.GetPolicyOffset()]; InputStream.Read(data, 0, data.Length); if (IsEncrypted(policies)) { // get the salt byte[] salt = GetSalt(); // decrypt the key and header TransformBuffer(data, salt); Array.Clear(salt, 0, salt.Length); } // copy to file keyWriter.Write(data, 0, data.Length); m_keyStream.SetLength(m_keyStream.Position); // clean up Array.Clear(data, 0, data.Length); InputStream.Dispose(); } catch { throw; } }
/// <remarks> /// Get the working copy of the key package as a stream /// </remarks> private MemoryStream GetKeyStream() { MemoryStream keyMem = null; try { m_keyStream.Seek(0, SeekOrigin.Begin); BinaryReader keyReader = new BinaryReader(m_keyStream); // output stream and writer keyMem = new MemoryStream((int)keyReader.BaseStream.Length); BinaryWriter keyWriter = new BinaryWriter(keyMem); // add policy flags KeyPolicy = keyReader.ReadInt64(); keyWriter.Write(KeyPolicy); // get the data byte[] data = keyReader.ReadBytes((int)(keyReader.BaseStream.Length - PackageKey.GetPolicyOffset())); // decrypt if (IsEncrypted(KeyPolicy)) { // get the salt byte[] salt = GetSalt(); // decrypt the key TransformBuffer(data, salt); // clear the salt Array.Clear(salt, 0, salt.Length); } // copy to stream keyWriter.Write(data); // don't wait for gc Array.Clear(data, 0, data.Length); // reset position keyMem.Seek(0, SeekOrigin.Begin); m_keyStream.Seek(0, SeekOrigin.Begin); return(keyMem); } catch { throw; } }
/// <summary> /// Create a key file using a <see cref="VTDev.Libraries.CEXEngine.Crypto.Processing.Structure.PackageKey"/> structure; containing the cipher description and operating ids and flags. /// </summary> /// /// <param name="Package">The PackageKeyKey containing the cipher description and operating ids and flags</param> /// <param name="SeedEngine">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Common.CipherDescription">Random Generator</see> used to create the stage 1 seed material during key generation.</param> /// <param name="DigestEngine">The <see cref="VTDev.Libraries.CEXEngine.Crypto.Common.CipherDescription">Digest Engine</see> used in the stage II phase of key generation.</param> /// /// <exception cref="CryptoProcessingException">Thrown if a key file exists at the path specified, the path is read only, the CipherDescription or KeyAuthority structures are invalid, or /// number of SubKeys specified is either less than 1 or more than the maximum allowed (100,000)</exception> public void Create(PackageKey Package, SeedGenerators SeedEngine = SeedGenerators.CSPRsg, Digests DigestEngine = Digests.SHA512) { // if you are getting exceptions.. read the docs! if (!CipherDescription.IsValid(Package.Description)) { throw new CryptoProcessingException("PackageFactory:Create", "The key package cipher settings are invalid!", new FormatException()); } if (!KeyAuthority.IsValid(Package.Authority)) { throw new CryptoProcessingException("PackageFactory:Create", "The key package key authority settings are invalid!", new FormatException()); } if (Package.SubKeyCount < 1) { throw new CryptoProcessingException("PackageFactory:Create", "The key package must contain at least 1 key!", new ArgumentOutOfRangeException()); } if (Package.SubKeyCount > SUBKEY_MAX) { throw new CryptoProcessingException("PackageFactory:Create", String.Format("The key package can not contain more than {0} keys!", SUBKEY_MAX), new ArgumentOutOfRangeException()); } // get the size of a subkey set int subKeySize = Package.Description.KeySize + EXTKEY_SIZE; if (Package.Description.IvSize > 0) { subKeySize += Package.Description.IvSize; } if (Package.Description.MacKeySize > 0) { subKeySize += Package.Description.MacKeySize; } if (subKeySize < 1) { throw new CryptoProcessingException("PackageFactory:Create", "The key package cipher settings are invalid!", new Exception()); } try { // store the auth struct and policy m_keyOwner = Package.Authority; KeyPolicy = Package.KeyPolicy; // get the serialized header byte[] header = Package.ToBytes(); // size key buffer byte[] buffer = new byte[subKeySize * Package.SubKeyCount]; // generate the keying material using (KeyGenerator keyGen = new KeyGenerator(SeedEngine, DigestEngine)) keyGen.GetBytes(buffer); BinaryWriter keyWriter = new BinaryWriter(m_keyStream); // pre-set the size to avoid fragmentation keyWriter.BaseStream.SetLength(PackageKey.GetHeaderSize(Package) + (subKeySize * Package.SubKeyCount)); if (IsEncrypted(Package.KeyPolicy)) { // add policy flags, only part of key not encrypted keyWriter.Write(Package.KeyPolicy); // get salt, return depends on auth flag settings byte[] salt = GetSalt(); // create a buffer for encrypted data int hdrLen = header.Length - PackageKey.GetPolicyOffset(); byte[] data = new byte[buffer.Length + hdrLen]; // copy header and key material Buffer.BlockCopy(header, PackageKey.GetPolicyOffset(), data, 0, hdrLen); Buffer.BlockCopy(buffer, 0, data, hdrLen, buffer.Length); // encrypt the key and header TransformBuffer(data, salt); // write to file keyWriter.Write(data); // don't wait for gc Array.Clear(salt, 0, salt.Length); Array.Clear(data, 0, data.Length); } else { // write the keypackage header keyWriter.Write(header, 0, header.Length); // write the keying material keyWriter.Write(buffer, 0, buffer.Length); } // cleanup m_keyStream.Seek(0, SeekOrigin.Begin); Array.Clear(header, 0, header.Length); Array.Clear(buffer, 0, buffer.Length); } catch (Exception) { throw; } }