Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add missing RijndaelKernel which is not autogenerated since it needs …

…fallbacks for non-AES block sizes
  • Loading branch information...
commit e6b6f798acd2b8f5378f5e7b801a0ecddd0347e8 1 parent ad0fc28
@spouliot spouliot authored
View
2  class/Crimson.CryptoDev/.gitignore
@@ -1,5 +1,5 @@
*.dll*
-Crimson.Security.Cryptography/
+Crimson.Security.Cryptography/*.g.cs
CryptoTools.cs
SymmetricTransform.cs
TestResult.xml
View
113 class/Crimson.CryptoDev/Crimson.Security.Cryptography/RijndaelKernel.cs
@@ -0,0 +1,113 @@
+//
+// Author:
+// Sebastien Pouliot <sebastien@gmail.com>
+//
+// Copyright 2012 Symform Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+// note: this file is not autogenerated since it's a special case where only
+// one (128 bits) block size will be handled by the kernel (if possible).
+// Everything else is is routed back to the managed fallback implementation.
+
+using System;
+using System.Security.Cryptography;
+using System.Runtime.InteropServices;
+
+using Mono.Security.Cryptography;
+using Crimson.CryptoDev;
+
+namespace Crimson.Security.Cryptography {
+
+ // since a lot of (older) code uses Rijndael for AES (as the default block
+ // size match the AES standard) we provide a RijndaelKernel implementation
+ // that will use the managed implementation for unsupported block size or
+ // cipher modes
+
+ public class RijndaelKernel : Rijndael {
+
+ //const int BufferBlockSize = Int32.MaxValue;
+ const int BufferBlockSize = 1936; // mv_cesa limit
+
+ public RijndaelKernel ()
+ {
+ }
+
+ public override void GenerateIV ()
+ {
+ IVValue = KeyBuilder.IV (BlockSizeValue >> 3);
+ }
+
+ public override void GenerateKey ()
+ {
+ KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
+ }
+
+ Rijndael Fallback ()
+ {
+ Rijndael r = new RijndaelManaged ();
+ r.Mode = Mode;
+ r.Padding = Padding;
+ r.BlockSize = BlockSize;
+ return r;
+ }
+
+ public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV)
+ {
+ try {
+ if (BlockSize == 128) {
+ switch (Mode) {
+ case CipherMode.CBC:
+ return new CryptoDevTransform (this, Cipher.AES_CBC, false, rgbKey, rgbIV, BufferBlockSize);
+ case CipherMode.ECB:
+ return new CryptoDevTransform (this, Cipher.AES_ECB, false, rgbKey, rgbIV, BufferBlockSize);
+ }
+ }
+ }
+ catch (CryptographicException) {
+ // the kernel might not have the required mode (even for 'generic') available
+ }
+ // fallback to default implementation for other, non-AES, block sizes
+ // cipher modes (effectivelty CFB)
+ return Fallback ().CreateDecryptor (rgbKey, rgbIV);
+ }
+
+ public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV)
+ {
+ try {
+ if (BlockSize == 128) {
+ switch (Mode) {
+ case CipherMode.CBC:
+ return new CryptoDevTransform (this, Cipher.AES_CBC, true, rgbKey, rgbIV, BufferBlockSize);
+ case CipherMode.ECB:
+ return new CryptoDevTransform (this, Cipher.AES_ECB, true, rgbKey, rgbIV, BufferBlockSize);
+ }
+ }
+ }
+ catch (CryptographicException) {
+ // the kernel might not have the required mode (even for 'generic') available
+ }
+ // fallback to default implementation for other, non-AES, block sizes
+ // cipher modes (effectivelty CFB)
+ return Fallback ().CreateEncryptor (rgbKey, rgbIV);
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.