Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #2 from symform/master

Fix a number of places where "fixed" keyword does not cover the IntPtr references.
  • Loading branch information...
commit d96fbede5db1e9b06c20cd7c592947f8c8127fc4 2 parents 5961278 + a6f18ab
Sebastien Pouliot authored September 24, 2012
45  class/Crimson.CryptoDev/Crimson.CryptoDev/CryptoDevTransform.cs
@@ -83,11 +83,16 @@ public CryptoDevTransform (SymmetricAlgorithm algo, Cipher cipher, bool encrypti
83 83
 			Session sess = new Session ();
84 84
 			sess.cipher = cipher;
85 85
 			sess.keylen = (uint) rgbKey.Length;
86  
-			fixed (byte* k = &rgbKey [0])
  86
+			fixed (byte* k = &rgbKey [0]) {
87 87
 				sess.key = (IntPtr) k;
88  
-
89  
-			if (Helper.SessionOp (ref sess) < 0)
90  
-				throw new CryptographicException (Marshal.GetLastWin32Error ());
  88
+				try {
  89
+					if (Helper.SessionOp (ref sess) < 0)
  90
+						throw new CryptographicException (Marshal.GetLastWin32Error ());
  91
+				}
  92
+				finally {
  93
+					sess.key = IntPtr.Zero;
  94
+				}
  95
+			}
91 96
 
92 97
 			context.ses = sess.ses;
93 98
 			context.op = encryption ? CryptoOperation.Encrypt : CryptoOperation.Decrypt;
@@ -98,8 +103,6 @@ public CryptoDevTransform (SymmetricAlgorithm algo, Cipher cipher, bool encrypti
98 103
 			if (algo.Mode != CipherMode.ECB) {
99 104
 				iv = rgbIV;
100 105
 				save_iv = new byte [BlockSizeByte];
101  
-				fixed (byte* i = &iv [0])
102  
-					context.iv = (IntPtr) i;
103 106
 			}
104 107
 
105 108
 			// transform buffer
@@ -151,26 +154,32 @@ void Transform (byte[] input, int inputOffset, byte[] output, int outputOffset,
151 154
 		{
152 155
 			while (length > 0) {
153 156
 				int size = Math.Min (length, BufferBlockSize);
154  
-				if (iv != null) {
155  
-					fixed (byte *i = &iv [0])
156  
-						context.iv = (IntPtr) i;
  157
+				fixed (byte *v = iv)
  158
+				fixed (byte *i = &input [inputOffset])
  159
+				fixed (byte *o = &output [outputOffset]) {
  160
+					if (iv != null) {
  161
+						context.iv = (IntPtr) v;
157 162
 
158  
-					if (!encrypt) {
159  
-						int ivOffset = inputOffset + size - BlockSizeByte;
160  
-						Buffer.BlockCopy (input, ivOffset, save_iv, 0, BlockSizeByte);
  163
+						if (!encrypt) {
  164
+							int ivOffset = inputOffset + size - BlockSizeByte;
  165
+							Buffer.BlockCopy (input, ivOffset, save_iv, 0, BlockSizeByte);
  166
+						}
161 167
 					}
162  
-				}
163 168
 
164  
-				fixed (byte *i = &input [inputOffset])
165  
-				fixed (byte *o = &output [outputOffset]) {
166 169
 					context.len = (uint) size;
167 170
 					context.src = (IntPtr) i;
168 171
 					context.dst = (IntPtr) o;
  172
+					try {
  173
+						if (Helper.CryptOp (ref context) < 0)
  174
+							throw new CryptographicException (Marshal.GetLastWin32Error ());
  175
+					}
  176
+					finally {
  177
+						context.iv = IntPtr.Zero;
  178
+						context.src = IntPtr.Zero;
  179
+						context.dst = IntPtr.Zero;
  180
+					}
169 181
 				}
170 182
 
171  
-				if (Helper.CryptOp (ref context) < 0)
172  
-					throw new CryptographicException (Marshal.GetLastWin32Error ());
173  
-
174 183
 				if (iv != null) {
175 184
 					if (encrypt)
176 185
 						Buffer.BlockCopy (output, outputOffset + size - BlockSizeByte, iv, 0, BlockSizeByte);
19  class/Crimson.CryptoDev/Crimson.CryptoDev/HashHelper.cs
@@ -78,9 +78,14 @@ public void Update (byte[] data, int start, int length)
78 78
 					context.len = (uint) size;
79 79
 					context.src = (IntPtr) p;
80 80
 					context.flags = CryptoFlags.Update;
  81
+					try {
  82
+						if (Helper.CryptOp (ref context) < 0)
  83
+							throw new CryptographicException (Marshal.GetLastWin32Error ());
  84
+					}
  85
+					finally {
  86
+						context.src = IntPtr.Zero;
  87
+					}
81 88
 				}
82  
-				if (Helper.CryptOp (ref context) < 0)
83  
-					throw new CryptographicException (Marshal.GetLastWin32Error ());
84 89
 				length -= size;
85 90
 				start += size;
86 91
 			}
@@ -93,11 +98,15 @@ public byte[] Final (int hashSize)
93 98
 				context.len = 0;
94 99
 				context.src = IntPtr.Zero;
95 100
 				context.mac = (IntPtr) p;
  101
+				try {
  102
+					if (Helper.CryptOp (ref context) < 0)
  103
+						throw new CryptographicException (Marshal.GetLastWin32Error ());
  104
+				}
  105
+				finally {
  106
+					context.mac = IntPtr.Zero;
  107
+				}
96 108
 			}
97  
-			if (Helper.CryptOp (ref context) < 0)
98  
-				throw new CryptographicException (Marshal.GetLastWin32Error ());
99 109
 
100  
-			context.mac = IntPtr.Zero;
101 110
 			return digest;
102 111
 		}		
103 112
 	}
64  class/Crimson.CryptoDev/Crimson.CryptoDev/Helper.cs
@@ -109,40 +109,42 @@ static public bool IsAvailable (Cipher algo)
109 109
 		
110 110
 		static bool Is (Cipher algo, KernelMode mode)
111 111
 		{
112  
-			Session session = new Session ();
113  
-			switch (algo) {
114  
-			case Cipher.AES_CBC:
115  
-			case Cipher.AES_ECB:
116  
-				session.cipher = algo;
117  
-				session.keylen = 32;
118  
-				fixed (byte* k = &null_key[0])
  112
+			fixed (byte* k = &null_key[0])
  113
+			{
  114
+				Session session = new Session ();
  115
+				switch (algo) {
  116
+				case Cipher.AES_CBC:
  117
+				case Cipher.AES_ECB:
  118
+					session.cipher = algo;
  119
+					session.keylen = 32;
119 120
 					session.key = (IntPtr)k;
120  
-				break;
121  
-			case Cipher.SHA1:
122  
-				session.mac = algo;
123  
-				break;
124  
-			// accept both SHA256 and SHA2_256 and use the correct one
125  
-			case Cipher.SHA256:
126  
-			case Cipher.SHA2_256:
127  
-				if (mode == KernelMode.Ocf)
128  
-					session.mac = Cipher.SHA2_256;
129  
-				else
130  
-					session.mac = Cipher.SHA256;
131  
-				break;
132  
-			default:
133  
-				return false;
134  
-			}
  121
+					break;
  122
+				case Cipher.SHA1:
  123
+					session.mac = algo;
  124
+					break;
  125
+				// accept both SHA256 and SHA2_256 and use the correct one
  126
+				case Cipher.SHA256:
  127
+				case Cipher.SHA2_256:
  128
+					if (mode == KernelMode.Ocf)
  129
+						session.mac = Cipher.SHA2_256;
  130
+					else
  131
+						session.mac = Cipher.SHA256;
  132
+					break;
  133
+				default:
  134
+					return false;
  135
+				}
135 136
 
136  
-			ulong ciocgsession = mode == KernelMode.CryptoDev ? CD_CIOCGSESSION : OCF_CIOCGSESSION;
137  
-			bool result;
138  
-			if (IntPtr.Size == 4)
139  
-				result = ioctl32 (fildes, (int) ciocgsession, ref session) == 0;
140  
-			else
141  
-				result = ioctl64 (fildes, ciocgsession, ref session) == 0;
  137
+				ulong ciocgsession = mode == KernelMode.CryptoDev ? CD_CIOCGSESSION : OCF_CIOCGSESSION;
  138
+				bool result;
  139
+				if (IntPtr.Size == 4)
  140
+					result = ioctl32 (fildes, (int) ciocgsession, ref session) == 0;
  141
+				else
  142
+					result = ioctl64 (fildes, ciocgsession, ref session) == 0;
142 143
 			
143  
-			if (result)
144  
-				Mode = mode;
145  
-			return result;
  144
+				if (result)
  145
+					Mode = mode;
  146
+				return result;
  147
+			}
146 148
 		}
147 149
 		
148 150
 		// values varies for cryptodev and OCF and for 32/64 bits

0 notes on commit d96fbed

Please sign in to comment.
Something went wrong with that request. Please try again.