コード例 #1
0
        internal ThreefishTransform(byte[] key, byte[] iv, int feedbackSize, ThreefishTransformMode transformMode,
                                    CipherMode cipherMode, PaddingMode paddingMode)
        {
            this.transformMode = transformMode;
            this.cipherMode    = cipherMode;
            this.paddingMode   = paddingMode;
            cipherBytes        = key.Length;
            cipherWords        = key.Length / 8;
            feedbackBytes      = feedbackSize / 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];
            depadBuffer = new byte[cipherBytes];
            this.iv     = new ulong[cipherWords];
            GetBytes(iv, 0, this.iv, cipherBytes);
            switch (OutputBlockSize)
            {
            case 256 / 8: cipher = new Threefish256(); break;

            case 512 / 8: cipher = new Threefish512(); break;

            case 1024 / 8: cipher = new Threefish1024(); break;

            default: throw new CryptographicException("Unsupported key/block size.");
            }
            bool e = transformMode == ThreefishTransformMode.Encrypt;

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

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

            case CipherMode.OFB:
                transformFunc = OfbApplyStream;
                break;

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

            default:
                throw new CryptographicException("Unsupported cipher mode.");
            }
            var keyWords = new ulong[cipherWords];

            GetBytes(key, 0, keyWords, cipherBytes);
            cipher.SetKey(keyWords);
            InitializeBlocks();
        }
コード例 #2
0
 private void ProcessBlock(int bytes)
 {
     // Set the key to the current state
     cipher.SetKey(state);
     // Update tweak
     UbiParameters.BitsProcessed += (ulong)bytes;
     cipher.SetTweak(UbiParameters.Tweak);
     // Encrypt block
     cipher.Encrypt(cipherInput, state);
     // Feed-forward input with state
     for (int i = 0; i < cipherInput.Length; i++)
     {
         state[i] ^= cipherInput[i];
     }
 }
コード例 #3
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();
        }
コード例 #4
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 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();
        }