public ThreefishTransform(
            byte[] key, byte[] iv, ThreefishTransformType type, CipherMode mode, PaddingMode padding
        )
        {
            _cipherMode    = mode;
            _paddingMode   = padding;

            _cipherBytes = key.Length;
            _cipherWords = key.Length / 8;
            OutputBlockSize  = key.Length * 8;

            // Allocate working blocks now so that we don't
            // have to allocate them each time
            // Transform(Final)Block is called
            _block = new ulong[_cipherWords];
            _tempBlock = new ulong[_cipherWords];
            _streamBytes = new byte[_cipherBytes];

            // Allocate IV and set value
            _iv = new ulong[_cipherWords];
            GetBytes(iv, 0, _iv, _cipherBytes);

            // Figure out which cipher we need based on
            // the cipher bit size
            switch (OutputBlockSize)
            {
                case 256:
                    _cipher = new Threefish256();
                    break;
                case 512:
                    _cipher = new Threefish512();
                    break;
                case 1024:
                    _cipher = new Threefish1024();
                    break;

                default:
                    throw new CryptographicException("Unsupported key/block size.");
            }

            bool e = (type == ThreefishTransformType.Encrypt);

            switch(_cipherMode)
            {
                case CipherMode.ECB:
                    _transformFunc = e ? new TransformFunc(EcbEncrypt) : new TransformFunc(EcbDecrypt);
                    break;
                case CipherMode.CBC:
                    _transformFunc = e ? new TransformFunc(CbcEncrypt) : new TransformFunc(CbcDecrypt);
                    break;
                case CipherMode.OFB:
                    _transformFunc = new TransformFunc(OfbApplyStream);
                    break;
                case CipherMode.CFB:
                    _transformFunc = e ? new TransformFunc(CfbEncrypt) : new TransformFunc(CfbDecrypt);
                    break;
                case CipherMode.CTS:
                    throw new CryptographicException("CTS mode not supported.");
            }

            // Set the key
            var keyWords = new ulong[_cipherWords];
            GetBytes(key, 0, keyWords, _cipherBytes);
            _cipher.SetKey(keyWords);

            InitializeBlocks();
        }
Exemple #2
0
        public ThreefishTransform(
            byte[] key, byte[] iv, ThreefishTransformType type, CipherMode mode, PaddingMode padding
            )
        {
            _cipherMode  = mode;
            _paddingMode = padding;

            _cipherBytes    = key.Length;
            _cipherWords    = key.Length / 8;
            OutputBlockSize = key.Length;// *8;

            // Allocate working blocks now so that we don't
            // have to allocate them each time
            // Transform(Final)Block is called
            _block       = new ulong[_cipherWords];
            _tempBlock   = new ulong[_cipherWords];
            _streamBytes = new byte[_cipherBytes];

            // Allocate IV and set value
            _iv = new ulong[_cipherWords];
            GetBytes(iv, 0, _iv, _cipherBytes);

            // Figure out which cipher we need based on
            // the cipher bit size
            switch (OutputBlockSize)
            {
            case 32:
                _cipher = new Threefish256();
                break;

            case 64:
                _cipher = new Threefish512();
                break;

            case 128:
                _cipher = new Threefish1024();
                break;

            default:
                throw new CryptographicException("Unsupported key/block size.");
            }

            bool e = (type == ThreefishTransformType.Encrypt);

            switch (_cipherMode)
            {
            case CipherMode.ECB:
                _transformFunc = e ? new TransformFunc(EcbEncrypt) : new TransformFunc(EcbDecrypt);
                break;

            case CipherMode.CBC:
                _transformFunc = e ? new TransformFunc(CbcEncrypt) : new TransformFunc(CbcDecrypt);
                break;

            case CipherMode.OFB:
                _transformFunc = new TransformFunc(OfbApplyStream);
                break;

            case CipherMode.CFB:
                _transformFunc = e ? new TransformFunc(CfbEncrypt) : new TransformFunc(CfbDecrypt);
                break;

            case CipherMode.CTS:
                throw new CryptographicException("CTS mode not supported.");
            }

            // Set the key
            var keyWords = new ulong[_cipherWords];

            GetBytes(key, 0, keyWords, _cipherBytes);
            _cipher.SetKey(keyWords);

            InitializeBlocks();
        }
        public ThreefishTransform(
            byte[] key, byte[] iv, ThreefishTransformType type, CipherMode mode, PaddingMode padding
            )
        {
            m_TransformType = type;
            m_CipherMode    = mode;
            m_PaddingMode   = padding;

            m_CipherBytes = key.Length;
            m_CipherWords = key.Length / 8;
            m_CipherBits  = key.Length * 8;

            // Allocate working blocks now so that we don't
            // have to allocate them each time
            // Transform(Final)Block is called
            m_Block = new ulong[m_CipherWords];
            m_TempBlock = new ulong[m_CipherWords];
            m_StreamBytes = new byte[m_CipherBytes];

            // Allocate IV and set value
            m_IV = new ulong[m_CipherWords];
            GetBytes(iv, 0, m_IV, m_CipherBytes);

            // Figure out which cipher we need based on
            // the cipher bit size
            switch (m_CipherBits)
            {
                case 256:
                    m_Cipher = new Threefish256();
                    break;
                case 512:
                    m_Cipher = new Threefish512();
                    break;
                case 1024:
                    m_Cipher = new Threefish1024();
                    break;

                default:
                    throw new CryptographicException("Unsupported key/block size.");
            }

            bool e = (type == ThreefishTransformType.Encrypt);

            switch(m_CipherMode)
            {
                case CipherMode.ECB:
                    m_TransformFunc = e ? new TransformFunc(ECB_Encrypt) : new TransformFunc(ECB_Decrypt);
                    break;
                case CipherMode.CBC:
                    m_TransformFunc = e ? new TransformFunc(CBC_Encrypt) : new TransformFunc(CBC_Decrypt);
                    break;
                case CipherMode.OFB:
                    m_TransformFunc = new TransformFunc(OFB_ApplyStream);
                    break;
                case CipherMode.CFB:
                    m_TransformFunc = e ? new TransformFunc(CFB_Encrypt) : new TransformFunc(CFB_Decrypt);
                    break;
                case CipherMode.CTS:
                    throw new CryptographicException("CTS mode not supported.");
            }

            // Set the key
            ulong[] key_words = new ulong[m_CipherWords];
            GetBytes(key, 0, key_words, m_CipherBytes);
            m_Cipher.SetKey(key_words);

            InitializeBlocks();
        }