Esempio n. 1
0
 /// <summary>
 /// Initializes the Skein hash instance.
 /// </summary>
 /// <param name="stateSize">The internal state size of the hash in bits.
 /// Supported values are 256, 512, and 1024.</param>
 /// <param name="outputSize">The output size of the hash in bits.
 /// Output size must be divisible by 8 and greater than zero.</param>
 public Skein(int stateSize, int outputSize)
 {
     if (outputSize <= 0)
     {
         throw new CryptographicException("Output bit size must be greater than zero.");
     }
     if (outputSize % 8 != 0)
     {
         throw new CryptographicException("Output bit size must be divisible by 8.");
     }
     cipherStateBits  = stateSize;
     cipherStateBytes = stateSize / 8;
     cipherStateWords = stateSize / 64;
     HashSizeValue    = outputSize;
     outputBytes      = (outputSize + 7) / 8;
     cipher           = ThreefishCipher.CreateCipher(stateSize);
     if (cipher == null)
     {
         throw new CryptographicException("Unsupported state size.");
     }
     inputBuffer   = new byte[cipherStateBytes];
     cipherInput   = new ulong[cipherStateWords];
     state         = new ulong[cipherStateWords];
     UbiParameters = new UbiTweak();
     Configuration = new SkeinConfig(this);
     Configuration.SetSchema(83, 72, 65, 51); // "SHA3"
     Configuration.SetVersion(1);
     Configuration.GenerateConfiguration();
     Initialize();
 }
Esempio n. 2
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();
        }
Esempio n. 3
0
        public void GenerateConfiguration()
        {
            var cipher = ThreefishCipher.CreateCipher(stateSize);
            var tweak  = new UbiTweak();

            tweak.StartNewBlockType(UbiType.Config);
            tweak.IsFinalBlock  = true;
            tweak.BitsProcessed = 32;
            cipher.SetTweak(tweak.Tweak);
            cipher.Encrypt(ConfigString, ConfigValue);
            ConfigValue[0] ^= ConfigString[0];
            ConfigValue[1] ^= ConfigString[1];
            ConfigValue[2] ^= ConfigString[2];
        }
Esempio n. 4
0
        /// <summary>
        /// Initializes the Skein hash instance.
        /// </summary>
        /// <param name="stateSize">The internal state size of the hash in bits.
        /// Supported values are 256, 512, and 1024.</param>
        /// <param name="outputSize">The output size of the hash in bits.
        /// Output size must be divisible by 8 and greater than zero.</param>
        public Skein(int stateSize, int outputSize)
        {
            // Make sure the output bit size > 0
            if (outputSize <= 0)
            {
                throw new CryptographicException("Output bit size must be greater than zero.");
            }

            // Make sure output size is divisible by 8
            if (outputSize % 8 != 0)
            {
                throw new CryptographicException("Output bit size must be divisible by 8.");
            }

            _cipherStateBits  = stateSize;
            _cipherStateBytes = stateSize / 8;
            _cipherStateWords = stateSize / 64;

            base.HashSizeValue = outputSize;
            _outputBytes       = (outputSize + 7) / 8;

            // Figure out which cipher we need based on
            // the state size
            _cipher = ThreefishCipher.CreateCipher(stateSize);
            if (_cipher == null)
            {
                throw new CryptographicException("Unsupported state size.");
            }

            // Allocate buffers
            _inputBuffer = new byte[_cipherStateBytes];
            _cipherInput = new ulong[_cipherStateWords];
            _state       = new ulong[_cipherStateWords];

            // Allocate tweak
            UbiParameters = new UbiTweak();

            // Generate the configuration string
            Configuration = new SkeinConfig(this);
            Configuration.SetSchema(83, 72, 65, 51); // "SHA3"
            Configuration.SetVersion(1);
            Configuration.GenerateConfiguration();
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes the Skein hash instance.
        /// </summary>
        /// <param name="stateSize">The internal state size of the hash in bits.
        /// Supported values are 256, 512, and 1024.</param>
        /// <param name="outputSize">The output size of the hash in bits.
        /// Output size must be divisible by 8 and greater than zero.</param>
        public Skein(int stateSize, int outputSize)
        {
            // Make sure the output bit size > 0
            if (outputSize <= 0)
                throw new CryptographicException("Output bit size must be greater than zero.");

            // Make sure output size is divisible by 8
            if (outputSize % 8 != 0)
                throw new CryptographicException("Output bit size must be divisible by 8.");

            _cipherStateBits = stateSize;
            _cipherStateBytes = stateSize / 8;
            _cipherStateWords = stateSize / 64;

            base.HashSizeValue = outputSize;
            _outputBytes = (outputSize + 7) / 8;

            // Figure out which cipher we need based on
            // the state size
            _cipher = ThreefishCipher.CreateCipher(stateSize);
            if (_cipher == null) throw new CryptographicException("Unsupported state size.");

            // Allocate buffers
            _inputBuffer = new byte[_cipherStateBytes];
            _cipherInput = new ulong[_cipherStateWords];
            _state = new ulong[_cipherStateWords];

            // Allocate tweak
            UbiParameters = new UbiTweak();

            // Generate the configuration string
            Configuration = new SkeinConfig(this);
            Configuration.SetSchema(83, 72, 65, 51); // "SHA3"
            Configuration.SetVersion(1);
            Configuration.GenerateConfiguration();
        }
Esempio n. 6
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();
        }
Esempio n. 7
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();
        }