Skip to content
Browse files

Style fixes to match mono's guidelines.

  • Loading branch information...
1 parent 2425868 commit 39237f1e65c5ad5e82c384bb079ccdfdd44cce3b @bassamtabbara bassamtabbara committed Oct 1, 2013
View
94 class/Crimson.OpenSsl/Crimson.OpenSsl/HashHelper.cs
@@ -23,61 +23,67 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-namespace Crimson.OpenSsl {
- using System;
+namespace Crimson.OpenSsl
+{
+ using System;
using System.Security.Cryptography;
- internal sealed class HashHelper : IDisposable {
- private readonly Native.SafeDigestContextHandle context;
- private readonly int hashSize;
+ internal sealed class HashHelper : IDisposable
+ {
+ private readonly Native.SafeDigestContextHandle context;
+ private readonly int hashSize;
- public HashHelper (Native.SafeDigestHandle digest, int hashSize) {
- this.hashSize = hashSize >> 3;
- if (this.hashSize > Native.MaximumDigestSize) {
- throw new ArgumentOutOfRangeException("hashSize");
- }
+ public HashHelper (Native.SafeDigestHandle digest, int hashSize)
+ {
+ this.hashSize = hashSize >> 3;
+ if (this.hashSize > Native.MaximumDigestSize) {
+ throw new ArgumentOutOfRangeException ("hashSize");
+ }
- this.context = Native.EVP_MD_CTX_create ();
- Native.ExpectSuccess (Native.EVP_DigestInit_ex (this.context, digest, IntPtr.Zero));
- }
+ this.context = Native.EVP_MD_CTX_create ();
+ Native.ExpectSuccess (Native.EVP_DigestInit_ex (this.context, digest, IntPtr.Zero));
+ }
- public void Dispose () {
- this.context.Dispose ();
- }
+ public void Dispose ()
+ {
+ this.context.Dispose ();
+ }
- public unsafe void Update (byte[] data, int start, int length) {
- if (length == 0) {
- return;
- }
+ public unsafe void Update (byte[] data, int start, int length)
+ {
+ if (length == 0) {
+ return;
+ }
- if (length < uint.MinValue) {
- throw new ArgumentOutOfRangeException("length");
- }
+ if (length < uint.MinValue) {
+ throw new ArgumentOutOfRangeException ("length");
+ }
- fixed (byte* p = &data[start]) {
- Native.ExpectSuccess (Native.EVP_DigestUpdate(this.context, (IntPtr)p, (uint)length));
- }
- }
+ fixed (byte* p = &data[start]) {
+ Native.ExpectSuccess (Native.EVP_DigestUpdate (this.context, (IntPtr)p, (uint)length));
+ }
+ }
- public unsafe byte[] Final () {
- var digest = new byte[Native.MaximumDigestSize];
- uint len;
+ public unsafe byte[] Final ()
+ {
+ var digest = new byte[Native.MaximumDigestSize];
+ uint len;
- fixed (byte* p = &digest[0]) {
- Native.ExpectSuccess (Native.EVP_DigestFinal_ex (this.context, (IntPtr)p, out len));
- }
+ fixed (byte* p = &digest[0]) {
+ Native.ExpectSuccess (Native.EVP_DigestFinal_ex (this.context, (IntPtr)p, out len));
+ }
- if (len != this.hashSize) {
- throw new CryptographicException (string.Format ("Mismatched hash length was expecting {0} but got {1}", this.hashSize, len));
- }
+ if (len != this.hashSize) {
+ throw new CryptographicException (string.Format ("Mismatched hash length was expecting {0} but got {1}", this.hashSize, len));
+ }
- if (len == digest.Length) {
- return digest;
- }
+ if (len == digest.Length) {
+ return digest;
+ }
- var trimmed = new byte[this.hashSize];
- Buffer.BlockCopy (digest, 0, trimmed, 0, this.hashSize);
- return trimmed;
- }
- }
+ var trimmed = new byte[this.hashSize];
+ Buffer.BlockCopy (digest, 0, trimmed, 0, this.hashSize);
+ return trimmed;
+ }
+ }
}
View
354 class/Crimson.OpenSsl/Crimson.OpenSsl/Native.cs
@@ -23,175 +23,187 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-
-namespace Crimson.OpenSsl {
- using Microsoft.Win32.SafeHandles;
- using System;
- using System.Runtime.InteropServices;
- using System.Security.Cryptography;
-
- /// <summary>
- /// Documentation can be found here: http://www.openssl.org/docs/crypto/EVP_DigestInit.html
- /// </summary>
- internal static class Native {
- const string DllName = "libcrypto";
-
- public static void ExpectSuccess (bool ret) {
- if (!ret) {
- throw new CryptographicException ();
- }
- }
-
- //
- // Version
- //
-
- [Serializable]
- public enum SSLeayVersionType {
- SSLEAY_VERSION = 0,
- SSLEAY_CFLAGS = 2,
- SSLEAY_BUILT_ON = 3,
- SSLEAY_PLATFORM = 4,
- SSLEAY_DIR = 5,
- }
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false, CharSet = CharSet.Ansi)]
- public extern static IntPtr SSLeay_version (SSLeayVersionType type);
-
- //
- // Digests
- //
-
- public const int MaximumDigestSize = 64;
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- public extern static SafeDigestHandle EVP_md5 ();
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- public extern static SafeDigestHandle EVP_sha1 ();
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- public extern static SafeDigestHandle EVP_sha256 ();
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- public extern static SafeDigestContextHandle EVP_MD_CTX_create ();
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- private extern static void EVP_MD_CTX_destroy (IntPtr ctx);
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- [return: MarshalAs(UnmanagedType.Bool)]
- public extern static bool EVP_DigestInit_ex(SafeDigestContextHandle ctx, SafeDigestHandle type, IntPtr impl);
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- [return: MarshalAs(UnmanagedType.Bool)]
- public extern static bool EVP_DigestUpdate(SafeDigestContextHandle ctx, IntPtr d, uint cnt);
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- [return: MarshalAs(UnmanagedType.Bool)]
- public extern static bool EVP_DigestFinal_ex(SafeDigestContextHandle ctx, IntPtr md, out uint s);
-
- internal sealed class SafeDigestHandle : SafeHandleZeroOrMinusOneIsInvalid {
- private SafeDigestHandle () :
- base (false) {
- }
-
- protected override bool ReleaseHandle () {
- return false;
- }
- }
-
- internal sealed class SafeDigestContextHandle : SafeHandleZeroOrMinusOneIsInvalid {
- internal SafeDigestContextHandle (IntPtr handle, bool ownsHandle) :
- base(ownsHandle) {
- this.SetHandle(handle);
- }
-
- private SafeDigestContextHandle () :
- base (true) {
- }
-
- protected override bool ReleaseHandle () {
- EVP_MD_CTX_destroy (this.handle);
- return true;
- }
- }
-
- //
- // Ciphers
- //
-
- [Serializable]
- public enum CipherOperation {
- Unchanged = -1,
- Decrypt = 0,
- Encrypt = 1,
- }
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- public extern static SafeCipherHandle EVP_aes_128_cbc ();
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- public extern static SafeCipherHandle EVP_aes_192_cbc ();
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- public extern static SafeCipherHandle EVP_aes_256_cbc ();
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- public extern static SafeCipherHandle EVP_aes_128_ecb ();
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- public extern static SafeCipherHandle EVP_aes_192_ecb ();
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- public extern static SafeCipherHandle EVP_aes_256_ecb ();
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- public extern static SafeCipherContextHandle EVP_CIPHER_CTX_new ();
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- private extern static void EVP_CIPHER_CTX_free (IntPtr a);
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- [return: MarshalAs(UnmanagedType.Bool)]
- public extern static bool EVP_CIPHER_CTX_set_key_length(SafeCipherContextHandle x, int keylen);
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- [return: MarshalAs(UnmanagedType.Bool)]
- public extern static bool EVP_CIPHER_CTX_set_padding(SafeCipherContextHandle x, int padding);
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- [return: MarshalAs(UnmanagedType.Bool)]
- public extern static bool EVP_CipherInit_ex(SafeCipherContextHandle ctx, SafeCipherHandle type, IntPtr impl, IntPtr key, IntPtr iv, CipherOperation enc);
-
- [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
- [return: MarshalAs (UnmanagedType.Bool)]
- public extern static bool EVP_CipherUpdate(SafeCipherContextHandle ctx, IntPtr outb, out int outl, IntPtr inb, int inl);
-
- internal sealed class SafeCipherHandle : SafeHandleZeroOrMinusOneIsInvalid {
- private SafeCipherHandle () :
- base (false) {
- }
-
- protected override bool ReleaseHandle () {
- return false;
- }
- }
-
- internal sealed class SafeCipherContextHandle : SafeHandleZeroOrMinusOneIsInvalid {
- internal SafeCipherContextHandle (IntPtr handle, bool ownsHandle) :
- base (ownsHandle) {
- this.SetHandle(handle);
- }
-
- private SafeCipherContextHandle () :
- base (true) {
- }
-
- protected override bool ReleaseHandle () {
- EVP_CIPHER_CTX_free (this.handle);
- return true;
- }
- }
- }
+namespace Crimson.OpenSsl
+{
+ using Microsoft.Win32.SafeHandles;
+ using System;
+ using System.Runtime.InteropServices;
+ using System.Security.Cryptography;
+
+ /// <summary>
+ /// Documentation can be found here: http://www.openssl.org/docs/crypto/EVP_DigestInit.html
+ /// </summary>
+ internal static class Native
+ {
+ const string DllName = "libcrypto";
+
+ public static void ExpectSuccess (bool ret)
+ {
+ if (!ret) {
+ throw new CryptographicException ();
+ }
+ }
+ //
+ // Version
+ //
+ [Serializable]
+ public enum SSLeayVersionType
+ {
+ SSLEAY_VERSION = 0,
+ SSLEAY_CFLAGS = 2,
+ SSLEAY_BUILT_ON = 3,
+ SSLEAY_PLATFORM = 4,
+ SSLEAY_DIR = 5,
+ }
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false, CharSet = CharSet.Ansi)]
+ public extern static IntPtr SSLeay_version (SSLeayVersionType type);
+ //
+ // Digests
+ //
+ public const int MaximumDigestSize = 64;
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ public extern static SafeDigestHandle EVP_md5 ();
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ public extern static SafeDigestHandle EVP_sha1 ();
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ public extern static SafeDigestHandle EVP_sha256 ();
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ public extern static SafeDigestContextHandle EVP_MD_CTX_create ();
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ private extern static void EVP_MD_CTX_destroy (IntPtr ctx);
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ public extern static bool EVP_DigestInit_ex (SafeDigestContextHandle ctx, SafeDigestHandle type, IntPtr impl);
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ public extern static bool EVP_DigestUpdate (SafeDigestContextHandle ctx, IntPtr d, uint cnt);
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ public extern static bool EVP_DigestFinal_ex (SafeDigestContextHandle ctx, IntPtr md, out uint s);
+
+ internal sealed class SafeDigestHandle : SafeHandleZeroOrMinusOneIsInvalid
+ {
+ private SafeDigestHandle () :
+ base (false)
+ {
+ }
+
+ protected override bool ReleaseHandle ()
+ {
+ return false;
+ }
+ }
+
+ internal sealed class SafeDigestContextHandle : SafeHandleZeroOrMinusOneIsInvalid
+ {
+ internal SafeDigestContextHandle (IntPtr handle, bool ownsHandle) :
+ base(ownsHandle)
+ {
+ this.SetHandle (handle);
+ }
+
+ private SafeDigestContextHandle () :
+ base (true)
+ {
+ }
+
+ protected override bool ReleaseHandle ()
+ {
+ EVP_MD_CTX_destroy (this.handle);
+ return true;
+ }
+ }
+ //
+ // Ciphers
+ //
+ [Serializable]
+ public enum CipherOperation
+ {
+ Unchanged = -1,
+ Decrypt = 0,
+ Encrypt = 1,
+ }
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ public extern static SafeCipherHandle EVP_aes_128_cbc ();
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ public extern static SafeCipherHandle EVP_aes_192_cbc ();
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ public extern static SafeCipherHandle EVP_aes_256_cbc ();
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ public extern static SafeCipherHandle EVP_aes_128_ecb ();
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ public extern static SafeCipherHandle EVP_aes_192_ecb ();
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ public extern static SafeCipherHandle EVP_aes_256_ecb ();
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ public extern static SafeCipherContextHandle EVP_CIPHER_CTX_new ();
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ private extern static void EVP_CIPHER_CTX_free (IntPtr a);
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ public extern static bool EVP_CIPHER_CTX_set_key_length (SafeCipherContextHandle x, int keylen);
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ public extern static bool EVP_CIPHER_CTX_set_padding (SafeCipherContextHandle x, int padding);
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ public extern static bool EVP_CipherInit_ex (SafeCipherContextHandle ctx, SafeCipherHandle type, IntPtr impl, IntPtr key, IntPtr iv, CipherOperation enc);
+
+ [DllImport (DllName, CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
+ [return: MarshalAs (UnmanagedType.Bool)]
+ public extern static bool EVP_CipherUpdate (SafeCipherContextHandle ctx, IntPtr outb, out int outl, IntPtr inb, int inl);
+
+ internal sealed class SafeCipherHandle : SafeHandleZeroOrMinusOneIsInvalid
+ {
+ private SafeCipherHandle () :
+ base (false)
+ {
+ }
+
+ protected override bool ReleaseHandle ()
+ {
+ return false;
+ }
+ }
+
+ internal sealed class SafeCipherContextHandle : SafeHandleZeroOrMinusOneIsInvalid
+ {
+ internal SafeCipherContextHandle (IntPtr handle, bool ownsHandle) :
+ base (ownsHandle)
+ {
+ this.SetHandle (handle);
+ }
+
+ private SafeCipherContextHandle () :
+ base (true)
+ {
+ }
+
+ protected override bool ReleaseHandle ()
+ {
+ EVP_CIPHER_CTX_free (this.handle);
+ return true;
+ }
+ }
+ }
}
View
149 class/Crimson.OpenSsl/Crimson.OpenSsl/OpenSslCryptoTransform.cs
@@ -23,77 +23,80 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-
-namespace Crimson.OpenSsl {
- using System;
- using System.Security.Cryptography;
- using Crimson.Common;
-
- internal unsafe class OpenSslCryptoTransform : CryptoTransformBase {
- private readonly Native.SafeCipherContextHandle context;
-
- public OpenSslCryptoTransform (SymmetricAlgorithm algo, bool encrypt, byte[] rgbKey, byte[] rgbIV)
- : base (algo, encrypt, rgbKey, rgbIV) {
-
- OpenSslUtil.EnsureAvailability ();
-
- this.context = Native.EVP_CIPHER_CTX_new ();
-
- var cptr = this.GetCipher (algo.Mode, rgbKey.Length);
- var op = encrypt ? Native.CipherOperation.Encrypt : Native.CipherOperation.Decrypt;
-
- fixed (byte* pkey = &rgbKey[0])
- fixed (byte* piv = &iv[0]) {
- Native.ExpectSuccess (Native.EVP_CipherInit_ex (this.context, cptr, IntPtr.Zero, (IntPtr)pkey, (IntPtr)piv, op));
- }
-
- Native.ExpectSuccess (Native.EVP_CIPHER_CTX_set_key_length (this.context, rgbKey.Length));
- Native.ExpectSuccess (Native.EVP_CIPHER_CTX_set_padding (this.context, 0));
- }
-
- protected override void Dispose (bool disposing) {
- if (disposing) {
- this.context.Dispose();
- }
-
- base.Dispose(disposing);
- }
-
- protected override void Transform (byte[] inputBuffer, int inputOffset, byte[] outputBuffer, int outputOffset, int inputCount) {
- fixed (byte* input = &inputBuffer[inputOffset])
- fixed (byte* output = &outputBuffer[outputOffset]) {
- int outputCount;
- Native.ExpectSuccess (Native.EVP_CipherUpdate (this.context, (IntPtr)output, out outputCount, (IntPtr)input, inputCount));
- }
- }
-
- private Native.SafeCipherHandle GetCipher (CipherMode mode, int keyLength) {
- if (mode == CipherMode.CBC) {
- switch (keyLength) {
- case 16:
- return Native.EVP_aes_128_cbc ();
-
- case 24:
- return Native.EVP_aes_192_cbc ();
-
- case 32:
- return Native.EVP_aes_256_cbc ();
- }
- }
- else if (mode == CipherMode.ECB) {
- switch (keyLength) {
- case 16:
- return Native.EVP_aes_128_ecb ();
-
- case 24:
- return Native.EVP_aes_192_ecb ();
-
- case 32:
- return Native.EVP_aes_256_ecb ();
- }
- }
-
- throw new CryptographicException (string.Format ("{0} not supported", mode));
- }
- }
+namespace Crimson.OpenSsl
+{
+ using System;
+ using System.Security.Cryptography;
+ using Crimson.Common;
+
+ internal unsafe class OpenSslCryptoTransform : CryptoTransformBase
+ {
+ private readonly Native.SafeCipherContextHandle context;
+
+ public OpenSslCryptoTransform (SymmetricAlgorithm algo, bool encrypt, byte[] rgbKey, byte[] rgbIV)
+ : base (algo, encrypt, rgbKey, rgbIV)
+ {
+ OpenSslUtil.EnsureAvailability ();
+
+ this.context = Native.EVP_CIPHER_CTX_new ();
+
+ var cptr = this.GetCipher (algo.Mode, rgbKey.Length);
+ var op = encrypt ? Native.CipherOperation.Encrypt : Native.CipherOperation.Decrypt;
+
+ fixed (byte* pkey = &rgbKey[0])
+ fixed (byte* piv = &iv[0]) {
+ Native.ExpectSuccess (Native.EVP_CipherInit_ex (this.context, cptr, IntPtr.Zero, (IntPtr)pkey, (IntPtr)piv, op));
+ }
+
+ Native.ExpectSuccess (Native.EVP_CIPHER_CTX_set_key_length (this.context, rgbKey.Length));
+ Native.ExpectSuccess (Native.EVP_CIPHER_CTX_set_padding (this.context, 0));
+ }
+
+ protected override void Dispose (bool disposing)
+ {
+ if (disposing) {
+ this.context.Dispose ();
+ }
+
+ base.Dispose (disposing);
+ }
+
+ protected override void Transform (byte[] inputBuffer, int inputOffset, byte[] outputBuffer, int outputOffset, int inputCount)
+ {
+ fixed (byte* input = &inputBuffer[inputOffset])
+ fixed (byte* output = &outputBuffer[outputOffset]) {
+ int outputCount;
+ Native.ExpectSuccess (Native.EVP_CipherUpdate (this.context, (IntPtr)output, out outputCount, (IntPtr)input, inputCount));
+ }
+ }
+
+ private Native.SafeCipherHandle GetCipher (CipherMode mode, int keyLength)
+ {
+ if (mode == CipherMode.CBC) {
+ switch (keyLength) {
+ case 16:
+ return Native.EVP_aes_128_cbc ();
+
+ case 24:
+ return Native.EVP_aes_192_cbc ();
+
+ case 32:
+ return Native.EVP_aes_256_cbc ();
+ }
+ } else if (mode == CipherMode.ECB) {
+ switch (keyLength) {
+ case 16:
+ return Native.EVP_aes_128_ecb ();
+
+ case 24:
+ return Native.EVP_aes_192_ecb ();
+
+ case 32:
+ return Native.EVP_aes_256_ecb ();
+ }
+ }
+
+ throw new CryptographicException (string.Format ("{0} not supported", mode));
+ }
+ }
}
View
23 class/Crimson.OpenSsl/Crimson.OpenSsl/OpenSslUtil.cs
@@ -23,31 +23,34 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-
-namespace Crimson.OpenSsl {
+namespace Crimson.OpenSsl
+{
using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
- public class OpenSslUtil {
- public static bool IsAvailable () {
+ public class OpenSslUtil
+ {
+ public static bool IsAvailable ()
+ {
try {
- GetVersionInfo();
- }
- catch (DllNotFoundException) {
+ GetVersionInfo ();
+ } catch (DllNotFoundException) {
return false;
}
return true;
}
- public static void EnsureAvailability () {
+ public static void EnsureAvailability ()
+ {
if (!IsAvailable ()) {
- throw new CryptographicException("libcrypto was not found");
+ throw new CryptographicException ("libcrypto was not found");
}
}
- public static string GetVersionInfo () {
+ public static string GetVersionInfo ()
+ {
return Marshal.PtrToStringAnsi (Native.SSLeay_version (Native.SSLeayVersionType.SSLEAY_VERSION));
}
}
View
12 class/Crimson.OpenSsl/Tests/AesOpenSslTests.cs
@@ -23,22 +23,20 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-
using System;
using System.Security.Cryptography;
-
using Crimson.Security.Cryptography;
using Crimson.Test.Base;
-
using NUnit.Framework;
-namespace Crimson.Test.OpenSsl {
-
+namespace Crimson.Test.OpenSsl
+{
[TestFixture]
- public class AesKernelTest : AesTest {
+ public class AesKernelTest : AesTest
+ {
[SetUp]
- protected void SetUp ()
+ protected void SetUp ()
{
algo = Create (); // shared
}
View
9 class/Crimson.OpenSsl/Tests/OpenSslTests.cs
@@ -23,16 +23,15 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-
using System;
using System.IO;
-
using NUnit.Framework;
using Crimson.OpenSsl;
-namespace Crimson.Test.OpenSsl {
-
- static class CryptoDevTest {
+namespace Crimson.Test.OpenSsl
+{
+ static class CryptoDevTest
+ {
static public void EnsureAvailability ()
{
View
118 class/Crimson.OpenSsl/Tests/RijndaelOpenSslTests.cs
@@ -23,74 +23,68 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-
using System;
using System.Security.Cryptography;
-
using Crimson.Security.Cryptography;
using Crimson.Test.Base;
-
using NUnit.Framework;
-namespace Crimson.Test.OpenSsl {
-
- [TestFixture]
- public class RijndaelOpenSslTests : RijndaelTest
- {
- [SetUp]
- protected void SetUp()
- {
- this.algo = this.Create(); // shared
- }
-
- protected override SymmetricAlgorithm Create()
- {
- return new RijndaelOpenSsl();
- }
-
- static bool Compare(byte[] actual, byte[] expected)
- {
- if (actual == null)
- return (expected == null);
- if (expected == null)
- return false;
- if (actual.Length != expected.Length)
- return false;
- for (int i = 0; i < actual.Length; i++)
- {
- if (actual[i] != expected[i])
- return false;
- }
- return true;
- }
-
- [Test]
- public void CbcIvBlock()
- {
- byte[] key = this.algo.Key;
- byte[] iv = this.algo.IV;
- this.algo.Mode = CipherMode.CBC;
-
- // 1952 = max mv_cesa + one block
- for (int i = 16; i <= 1952; i += 16)
- {
- byte[] data = new byte[i];
- byte[] enc1 = this.algo.CreateEncryptor().TransformFinalBlock(data, 0, data.Length);
- byte[] enc2 = null;
- using (Rijndael r = new RijndaelManaged())
- {
- r.Mode = CipherMode.CBC;
- r.Key = key;
- r.IV = iv;
- enc2 = this.algo.CreateEncryptor().TransformFinalBlock(data, 0, data.Length);
- }
-
- if (!Compare(enc1, enc2))
- Assert.Fail("Data size = " + i);
- }
- }
-
- private static byte[] key1 = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
+namespace Crimson.Test.OpenSsl
+{
+ [TestFixture]
+ public class RijndaelOpenSslTests : RijndaelTest
+ {
+ [SetUp]
+ protected void SetUp ()
+ {
+ this.algo = this.Create (); // shared
+ }
+
+ protected override SymmetricAlgorithm Create ()
+ {
+ return new RijndaelOpenSsl ();
+ }
+
+ static bool Compare (byte[] actual, byte[] expected)
+ {
+ if (actual == null)
+ return (expected == null);
+ if (expected == null)
+ return false;
+ if (actual.Length != expected.Length)
+ return false;
+ for (int i = 0; i < actual.Length; i++) {
+ if (actual [i] != expected [i])
+ return false;
+ }
+ return true;
+ }
+
+ [Test]
+ public void CbcIvBlock ()
+ {
+ byte[] key = this.algo.Key;
+ byte[] iv = this.algo.IV;
+ this.algo.Mode = CipherMode.CBC;
+
+ // 1952 = max mv_cesa + one block
+ for (int i = 16; i <= 1952; i += 16) {
+ byte[] data = new byte[i];
+ byte[] enc1 = this.algo.CreateEncryptor ().TransformFinalBlock (data, 0, data.Length);
+ byte[] enc2 = null;
+ using (Rijndael r = new RijndaelManaged()) {
+ r.Mode = CipherMode.CBC;
+ r.Key = key;
+ r.IV = iv;
+ enc2 = this.algo.CreateEncryptor ().TransformFinalBlock (data, 0, data.Length);
+ }
+
+ if (!Compare (enc1, enc2))
+ Assert.Fail ("Data size = " + i);
+ }
+ }
+
+ private static byte[] key1 = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
private static byte[] key2 = { 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01 };
private static byte[] key3 = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
View
21 class/Crimson.OpenSsl/Tests/SHA1OpenSslTests.cs
@@ -23,24 +23,21 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-
using System;
using System.Security.Cryptography;
-
using Crimson.Security.Cryptography;
using Crimson.Test.Base;
-
using NUnit.Framework;
namespace Crimson.Test.OpenSsl
{
- [TestFixture]
- public class SHA1OpenSslTests : SHA1Test
- {
- [SetUp]
- protected void SetUp()
- {
- hash = new SHA1OpenSsl();
- }
- }
+ [TestFixture]
+ public class SHA1OpenSslTests : SHA1Test
+ {
+ [SetUp]
+ protected void SetUp ()
+ {
+ hash = new SHA1OpenSsl ();
+ }
+ }
}
View
21 class/Crimson.OpenSsl/Tests/SHA256OpenSslTests.cs
@@ -23,24 +23,21 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-
using System;
using System.Security.Cryptography;
-
using Crimson.Security.Cryptography;
using Crimson.Test.Base;
-
using NUnit.Framework;
namespace Crimson.Test.OpenSsl
{
- [TestFixture]
- public class SHA256OpenSslTests : SHA256Test
- {
- [SetUp]
- protected void SetUp()
- {
- hash = new SHA256OpenSsl();
- }
- }
+ [TestFixture]
+ public class SHA256OpenSslTests : SHA256Test
+ {
+ [SetUp]
+ protected void SetUp ()
+ {
+ hash = new SHA256OpenSsl ();
+ }
+ }
}

0 comments on commit 39237f1

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