예제 #1
0
파일: Skein.cs 프로젝트: neoeinstein/xpdm
        public Skein(int state_size, int output_size)
        {
            // Make sure the output bit size > 0
            if (output_size <= 0)
                throw new CryptographicException("Output bit size must be greater than zero.");

            m_CipherStateBits = state_size;
            m_CipherStateBytes = state_size / 8;
            m_CipherStateWords = state_size / 64;

            base.HashSizeValue = output_size;
            m_OutputBytes = (output_size + 7) / 8;

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

            // Allocate buffers
            m_InputBuffer = new byte[m_CipherStateBytes];
            m_CipherInput = new ulong[m_CipherStateWords];
            m_State = new ulong[m_CipherStateWords];

            // Allocate tweak
            m_Tweak = new UBITweak();

            // Set default payload type (regular straight hashing)
            m_PayloadType = UBIType.Message;

            // Generate the configuration string
            m_Configuration = new SkeinConfig(this);
            m_Configuration.SetSchema("SHA3");
            m_Configuration.SetVersion(1);
            m_Configuration.GenerateConfiguration();

            // Initialize hash
            Initialize();
        }
예제 #2
0
        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();
        }