SessionOp() static private method

static private SessionOp ( Crimson.CryptoDev.Session &session ) : int
session Crimson.CryptoDev.Session
return int
Ejemplo n.º 1
0
        public CryptoDevTransform(SymmetricAlgorithm algo, Cipher cipher, bool encryption, byte[] rgbKey, byte[] rgbIV, int bufferBlockSize)
            : base(algo, encryption, rgbKey, rgbIV)

        {
            if (!Helper.IsAvailable(cipher))
            {
                throw new CryptographicException(String.Format("{0} not available from /dev/crypto", algo));
            }

            // linux does not requires cloning the file descriptor with CRIOGET
            Session sess = new Session();

            sess.cipher = cipher;
            sess.keylen = (uint)rgbKey.Length;
            fixed(byte *k = &rgbKey[0])
            {
                sess.key = (IntPtr)k;
                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;
            // CryptoOperation constants differs in OCF (0 is None, ...)
            if (Helper.Mode == KernelMode.Ocf)
            {
                context.op++;
            }

            if (algo.Mode != CipherMode.ECB)
            {
                save_iv = new byte [BlockSizeByte];
            }

            // change this value if the driver (e.g. mv_cesa) has a limit that
            // it can process in a single shot (e.g. 1936 for AES)
            BufferBlockSize = bufferBlockSize;
        }
Ejemplo n.º 2
0
        public HashHelper(Cipher algo)
        {
            if (!Helper.IsAvailable(algo))
            {
                throw new CryptographicException(String.Format("{0} not available from /dev/crypto", algo));
            }

            // linux does not requires cloning the file descriptor with CRIOGET
            Session sess = new Session();

            sess.mac = algo;

            if (Helper.SessionOp(ref sess) < 0)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            context.ses = sess.ses;
            context.op  = CryptoOperation.Encrypt;
            // change this value if the driver (e.g. mv_cesa) has a limit that
            // it can process in a single shot (e.g. 1932 for SHA1)
            BufferBlockSize = Int32.MaxValue;
        }
Ejemplo n.º 3
0
        public CryptoDevTransform(SymmetricAlgorithm algo, Cipher cipher, bool encryption, byte[] rgbKey, byte[] rgbIV, int bufferBlockSize)
        {
            if (!Helper.IsAvailable(cipher))
            {
                throw new CryptographicException(String.Format("{0} not available from /dev/crypto", algo));
            }

            if (rgbKey == null)
            {
                throw new CryptographicException("Invalid (null) key");
            }

            BlockSizeByte = (algo.BlockSize >> 3);

            if (rgbIV == null)
            {
                rgbIV = KeyBuilder.IV(BlockSizeByte);
            }
            else
            {
                // compare the IV length with the "currently selected" block size and *ignore* IV that are too big
                if (rgbIV.Length < BlockSizeByte)
                {
                    string msg = Locale.GetText("IV is too small ({0} bytes), it should be {1} bytes long.",
                                                rgbIV.Length, BlockSizeByte);
                    throw new CryptographicException(msg);
                }
                rgbIV = (byte[])rgbIV.Clone();
            }

            encrypt = encryption;
            padding = algo.Padding;

            // linux does not requires cloning the file descriptor with CRIOGET
            Session sess = new Session();

            sess.cipher = cipher;
            sess.keylen = (uint)rgbKey.Length;

            fixed(byte *k = &rgbKey[0])
            sess.key = (IntPtr)k;

            if (Helper.SessionOp(ref sess) < 0)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            context.ses = sess.ses;
            context.op  = encryption ? CryptoOperation.Encrypt : CryptoOperation.Decrypt;
            // CryptoOperation constants differs in OCF (0 is None, ...)
            if (Helper.Mode == KernelMode.Ocf)
            {
                context.op++;
            }

            if (algo.Mode != CipherMode.ECB)
            {
                iv      = rgbIV;
                save_iv = new byte [BlockSizeByte];

                fixed(byte *i = &iv[0])
                context.iv = (IntPtr)i;
            }

            // transform buffer
            workBuff = new byte [BlockSizeByte];
            // change this value if the driver (e.g. mv_cesa) has a limit that
            // it can process in a single shot (e.g. 1936 for AES)
            BufferBlockSize = bufferBlockSize;
        }