예제 #1
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;
        }
 public override void GenerateIV()
 {
     IVValue = KeyBuilder.IV(BlockSizeValue >> 3);
 }
예제 #3
0
 public override void GenerateIV()
 {
     IVValue = KeyBuilder.IV(DESTransform.BLOCK_BYTE_SIZE);
 }