I am currently creating a cryptographic application for the Windows Store. This means extensive use of the CryptographicBuffer class. The application is very sensitive to security, so I would like to make sure that Zeroize any Buffer immediately after use. I use the same with byte[] when I use them.
To make them null, we currently want to:
- Write all 1.
- Write a template. Currently used 0,1,2, ... 254,255,0,1 ...
- Write all 0.
The solution I came up with is to create an extension method for each of IBuffer and byte[] , which will do this for me. For byte[] I find this pretty simple:
public static void Zeroize(this byte[] bytes) { for (int i = 0; i < bytes.Length; i++) { bytes[i] = 255; } for (int i = 0; i < bytes.Length; i++) { bytes[i] = (byte)(i % 255); } for (int i = 0; i < bytes.Length; i++) { bytes[i] = 0; } }
For IBuffer this is a little trickier, since you don't seem to get direct access to Buffer . Via System.Runtime.InteropServices.WindowsRuntime; you seem to get some useful methods like IBuffer.CopyTo and IBuffer.AsStream that give you direct access to Buffer or the base Stream . The solution I came up with is the following:
public static void Zeroize(this IBuffer buffer) { var capacity = buffer.Capacity; byte[] toWrite = new byte[capacity]; for (int i = 0; i < capacity; i++) { toWrite[i] = 255; } toWrite.AsBuffer().CopyTo(buffer); for (int i = 0; i < capacity; i++) { toWrite[i] = (byte)(i % 255); } toWrite.AsBuffer().CopyTo(buffer); for (int i = 0; i < capacity; i++) { toWrite[i] = 0; } toWrite.AsBuffer().CopyTo(buffer); }
My questions are: is there a better way to do this? Are there any other hidden methods ( InteropServices n't really advertised so well) that would make this any easier / more efficient / safer?
Note. I understand that the Zeroization process may be excessive, but it is requested by the Owner.
source share