Permalink
Browse files

Merge pull request #2 from symform/master

Fix a number of places where "fixed" keyword does not cover the IntPtr references.
  • Loading branch information...
2 parents 5961278 + a6f18ab commit d96fbede5db1e9b06c20cd7c592947f8c8127fc4 @spouliot spouliot committed Sep 25, 2012
@@ -83,11 +83,16 @@ public CryptoDevTransform (SymmetricAlgorithm algo, Cipher cipher, bool encrypti
Session sess = new Session ();
sess.cipher = cipher;
sess.keylen = (uint) rgbKey.Length;
- fixed (byte* k = &rgbKey [0])
+ fixed (byte* k = &rgbKey [0]) {
sess.key = (IntPtr) k;
-
- if (Helper.SessionOp (ref sess) < 0)
- throw new CryptographicException (Marshal.GetLastWin32Error ());
+ try {
+ if (Helper.SessionOp (ref sess) < 0)
+ throw new CryptographicException (Marshal.GetLastWin32Error ());
+ }
+ finally {
+ sess.key = IntPtr.Zero;
+ }
+ }
context.ses = sess.ses;
context.op = encryption ? CryptoOperation.Encrypt : CryptoOperation.Decrypt;
@@ -98,8 +103,6 @@ public CryptoDevTransform (SymmetricAlgorithm algo, Cipher cipher, bool encrypti
if (algo.Mode != CipherMode.ECB) {
iv = rgbIV;
save_iv = new byte [BlockSizeByte];
- fixed (byte* i = &iv [0])
- context.iv = (IntPtr) i;
}
// transform buffer
@@ -151,26 +154,32 @@ void Transform (byte[] input, int inputOffset, byte[] output, int outputOffset,
{
while (length > 0) {
int size = Math.Min (length, BufferBlockSize);
- if (iv != null) {
- fixed (byte *i = &iv [0])
- context.iv = (IntPtr) i;
+ fixed (byte *v = iv)
+ fixed (byte *i = &input [inputOffset])
+ fixed (byte *o = &output [outputOffset]) {
+ if (iv != null) {
+ context.iv = (IntPtr) v;
- if (!encrypt) {
- int ivOffset = inputOffset + size - BlockSizeByte;
- Buffer.BlockCopy (input, ivOffset, save_iv, 0, BlockSizeByte);
+ if (!encrypt) {
+ int ivOffset = inputOffset + size - BlockSizeByte;
+ Buffer.BlockCopy (input, ivOffset, save_iv, 0, BlockSizeByte);
+ }
}
- }
- fixed (byte *i = &input [inputOffset])
- fixed (byte *o = &output [outputOffset]) {
context.len = (uint) size;
context.src = (IntPtr) i;
context.dst = (IntPtr) o;
+ try {
+ if (Helper.CryptOp (ref context) < 0)
+ throw new CryptographicException (Marshal.GetLastWin32Error ());
+ }
+ finally {
+ context.iv = IntPtr.Zero;
+ context.src = IntPtr.Zero;
+ context.dst = IntPtr.Zero;
+ }
}
- if (Helper.CryptOp (ref context) < 0)
- throw new CryptographicException (Marshal.GetLastWin32Error ());
-
if (iv != null) {
if (encrypt)
Buffer.BlockCopy (output, outputOffset + size - BlockSizeByte, iv, 0, BlockSizeByte);
@@ -78,9 +78,14 @@ public void Update (byte[] data, int start, int length)
context.len = (uint) size;
context.src = (IntPtr) p;
context.flags = CryptoFlags.Update;
+ try {
+ if (Helper.CryptOp (ref context) < 0)
+ throw new CryptographicException (Marshal.GetLastWin32Error ());
+ }
+ finally {
+ context.src = IntPtr.Zero;
+ }
}
- if (Helper.CryptOp (ref context) < 0)
- throw new CryptographicException (Marshal.GetLastWin32Error ());
length -= size;
start += size;
}
@@ -93,11 +98,15 @@ public byte[] Final (int hashSize)
context.len = 0;
context.src = IntPtr.Zero;
context.mac = (IntPtr) p;
+ try {
+ if (Helper.CryptOp (ref context) < 0)
+ throw new CryptographicException (Marshal.GetLastWin32Error ());
+ }
+ finally {
+ context.mac = IntPtr.Zero;
+ }
}
- if (Helper.CryptOp (ref context) < 0)
- throw new CryptographicException (Marshal.GetLastWin32Error ());
- context.mac = IntPtr.Zero;
return digest;
}
}
@@ -109,40 +109,42 @@ static public bool IsAvailable (Cipher algo)
static bool Is (Cipher algo, KernelMode mode)
{
- Session session = new Session ();
- switch (algo) {
- case Cipher.AES_CBC:
- case Cipher.AES_ECB:
- session.cipher = algo;
- session.keylen = 32;
- fixed (byte* k = &null_key[0])
+ fixed (byte* k = &null_key[0])
+ {
+ Session session = new Session ();
+ switch (algo) {
+ case Cipher.AES_CBC:
+ case Cipher.AES_ECB:
+ session.cipher = algo;
+ session.keylen = 32;
session.key = (IntPtr)k;
- break;
- case Cipher.SHA1:
- session.mac = algo;
- break;
- // accept both SHA256 and SHA2_256 and use the correct one
- case Cipher.SHA256:
- case Cipher.SHA2_256:
- if (mode == KernelMode.Ocf)
- session.mac = Cipher.SHA2_256;
- else
- session.mac = Cipher.SHA256;
- break;
- default:
- return false;
- }
+ break;
+ case Cipher.SHA1:
+ session.mac = algo;
+ break;
+ // accept both SHA256 and SHA2_256 and use the correct one
+ case Cipher.SHA256:
+ case Cipher.SHA2_256:
+ if (mode == KernelMode.Ocf)
+ session.mac = Cipher.SHA2_256;
+ else
+ session.mac = Cipher.SHA256;
+ break;
+ default:
+ return false;
+ }
- ulong ciocgsession = mode == KernelMode.CryptoDev ? CD_CIOCGSESSION : OCF_CIOCGSESSION;
- bool result;
- if (IntPtr.Size == 4)
- result = ioctl32 (fildes, (int) ciocgsession, ref session) == 0;
- else
- result = ioctl64 (fildes, ciocgsession, ref session) == 0;
+ ulong ciocgsession = mode == KernelMode.CryptoDev ? CD_CIOCGSESSION : OCF_CIOCGSESSION;
+ bool result;
+ if (IntPtr.Size == 4)
+ result = ioctl32 (fildes, (int) ciocgsession, ref session) == 0;
+ else
+ result = ioctl64 (fildes, ciocgsession, ref session) == 0;
- if (result)
- Mode = mode;
- return result;
+ if (result)
+ Mode = mode;
+ return result;
+ }
}
// values varies for cryptodev and OCF and for 32/64 bits

0 comments on commit d96fbed

Please sign in to comment.