public int DoFinal(byte[] output, int outOff)
 {
     this.finish();
     for (int i = 0; i < 8; i++)
     {
         WhirlpoolDigest.convertLongToByteArray(this._hash[i], output, outOff + i * 8);
     }
     this.Reset();
     return(this.GetDigestSize());
 }
 private void processFilledBuffer()
 {
     for (int i = 0; i < this._state.Length; i++)
     {
         this._block[i] = WhirlpoolDigest.bytesToLongFromBuffer(this._buffer, i * 8);
     }
     this.processBlock();
     this._bufferPos = 0;
     Array.Clear(this._buffer, 0, this._buffer.Length);
 }
Example #3
0
 public WhirlpoolDigest(WhirlpoolDigest originalDigest)
 {
     this._rc       = new long[11];
     this._buffer   = new byte[0x40];
     this._bitCount = new short[0x20];
     this._hash     = new long[8];
     this._K        = new long[8];
     this._L        = new long[8];
     this._block    = new long[8];
     this._state    = new long[8];
     this.Reset(originalDigest);
 }
        public void Reset(IMemoable other)
        {
            WhirlpoolDigest whirlpoolDigest = (WhirlpoolDigest)other;

            Array.Copy(whirlpoolDigest._rc, 0, this._rc, 0, this._rc.Length);
            Array.Copy(whirlpoolDigest._buffer, 0, this._buffer, 0, this._buffer.Length);
            this._bufferPos = whirlpoolDigest._bufferPos;
            Array.Copy(whirlpoolDigest._bitCount, 0, this._bitCount, 0, this._bitCount.Length);
            Array.Copy(whirlpoolDigest._hash, 0, this._hash, 0, this._hash.Length);
            Array.Copy(whirlpoolDigest._K, 0, this._K, 0, this._K.Length);
            Array.Copy(whirlpoolDigest._L, 0, this._L, 0, this._L.Length);
            Array.Copy(whirlpoolDigest._block, 0, this._block, 0, this._block.Length);
            Array.Copy(whirlpoolDigest._state, 0, this._state, 0, this._state.Length);
        }
        public void Reset(IMemoable other)
        {
            WhirlpoolDigest whirlpoolDigest = (WhirlpoolDigest)other;

            global::System.Array.Copy((global::System.Array)whirlpoolDigest._rc, 0, (global::System.Array)_rc, 0, _rc.Length);
            global::System.Array.Copy((global::System.Array)whirlpoolDigest._buffer, 0, (global::System.Array)_buffer, 0, _buffer.Length);
            _bufferPos = whirlpoolDigest._bufferPos;
            global::System.Array.Copy((global::System.Array)whirlpoolDigest._bitCount, 0, (global::System.Array)_bitCount, 0, _bitCount.Length);
            global::System.Array.Copy((global::System.Array)whirlpoolDigest._hash, 0, (global::System.Array)_hash, 0, _hash.Length);
            global::System.Array.Copy((global::System.Array)whirlpoolDigest._K, 0, (global::System.Array)_K, 0, _K.Length);
            global::System.Array.Copy((global::System.Array)whirlpoolDigest._L, 0, (global::System.Array)_L, 0, _L.Length);
            global::System.Array.Copy((global::System.Array)whirlpoolDigest._block, 0, (global::System.Array)_block, 0, _block.Length);
            global::System.Array.Copy((global::System.Array)whirlpoolDigest._state, 0, (global::System.Array)_state, 0, _state.Length);
        }
        private long[] _state = new long[8];     // the current "cipher" state



        /**
         * Copy constructor. This will copy the state of the provided message
         * digest.
         */
        public WhirlpoolDigest(WhirlpoolDigest originalDigest)
        {
            Array.Copy(originalDigest._rc, 0, _rc, 0, _rc.Length);

            Array.Copy(originalDigest._buffer, 0, _buffer, 0, _buffer.Length);

            this._bufferPos = originalDigest._bufferPos;
            Array.Copy(originalDigest._bitCount, 0, _bitCount, 0, _bitCount.Length);

            // -- internal hash state --
            Array.Copy(originalDigest._hash, 0, _hash, 0, _hash.Length);
            Array.Copy(originalDigest._K, 0, _K, 0, _K.Length);
            Array.Copy(originalDigest._L, 0, _L, 0, _L.Length);
            Array.Copy(originalDigest._block, 0, _block, 0, _block.Length);
            Array.Copy(originalDigest._state, 0, _state, 0, _state.Length);
        }
Example #7
0
        public void Reset(IMemoable other)
        {
            WhirlpoolDigest originalDigest = (WhirlpoolDigest)other;

            Array.Copy(originalDigest._rc, 0, _rc, 0, _rc.Length);

            Array.Copy(originalDigest._buffer, 0, _buffer, 0, _buffer.Length);

            this._bufferPos = originalDigest._bufferPos;
            Array.Copy(originalDigest._bitCount, 0, _bitCount, 0, _bitCount.Length);

            // -- internal hash state --
            Array.Copy(originalDigest._hash, 0, _hash, 0, _hash.Length);
            Array.Copy(originalDigest._K, 0, _K, 0, _K.Length);
            Array.Copy(originalDigest._L, 0, _L, 0, _L.Length);
            Array.Copy(originalDigest._block, 0, _block, 0, _block.Length);
            Array.Copy(originalDigest._state, 0, _state, 0, _state.Length);
        }
Example #8
0
        private long[] _state = new long[8];     // the current "cipher" state



        /**
         * Copy constructor. This will copy the state of the provided message
         * digest.
         */
        public WhirlpoolDigest(WhirlpoolDigest originalDigest)
        {
            Reset(originalDigest);
        }
 static WhirlpoolDigest()
 {
     WhirlpoolDigest.SBOX = new int[]
     {
         24,
         35,
         198,
         232,
         135,
         184,
         1,
         79,
         54,
         166,
         210,
         245,
         121,
         111,
         145,
         82,
         96,
         188,
         155,
         142,
         163,
         12,
         123,
         53,
         29,
         224,
         215,
         194,
         46,
         75,
         254,
         87,
         21,
         119,
         55,
         229,
         159,
         240,
         74,
         218,
         88,
         201,
         41,
         10,
         177,
         160,
         107,
         133,
         189,
         93,
         16,
         244,
         203,
         62,
         5,
         103,
         228,
         39,
         65,
         139,
         167,
         125,
         149,
         216,
         251,
         238,
         124,
         102,
         221,
         23,
         71,
         158,
         202,
         45,
         191,
         7,
         173,
         90,
         131,
         51,
         99,
         2,
         170,
         113,
         200,
         25,
         73,
         217,
         242,
         227,
         91,
         136,
         154,
         38,
         50,
         176,
         233,
         15,
         213,
         128,
         190,
         205,
         52,
         72,
         255,
         122,
         144,
         95,
         32,
         104,
         26,
         174,
         180,
         84,
         147,
         34,
         100,
         241,
         115,
         18,
         64,
         8,
         195,
         236,
         219,
         161,
         141,
         61,
         151,
         0,
         207,
         43,
         118,
         130,
         214,
         27,
         181,
         175,
         106,
         80,
         69,
         243,
         48,
         239,
         63,
         85,
         162,
         234,
         101,
         186,
         47,
         192,
         222,
         28,
         253,
         77,
         146,
         117,
         6,
         138,
         178,
         230,
         14,
         31,
         98,
         212,
         168,
         150,
         249,
         197,
         37,
         89,
         132,
         114,
         57,
         76,
         94,
         120,
         56,
         140,
         209,
         165,
         226,
         97,
         179,
         33,
         156,
         30,
         67,
         199,
         252,
         4,
         81,
         153,
         109,
         13,
         250,
         223,
         126,
         36,
         59,
         171,
         206,
         17,
         143,
         78,
         183,
         235,
         60,
         129,
         148,
         247,
         185,
         19,
         44,
         211,
         231,
         110,
         196,
         3,
         86,
         68,
         127,
         169,
         42,
         187,
         193,
         83,
         220,
         11,
         157,
         108,
         49,
         116,
         246,
         70,
         172,
         137,
         20,
         225,
         22,
         58,
         105,
         9,
         112,
         182,
         208,
         237,
         204,
         66,
         152,
         164,
         40,
         92,
         248,
         134
     };
     WhirlpoolDigest.C0        = new long[256];
     WhirlpoolDigest.C1        = new long[256];
     WhirlpoolDigest.C2        = new long[256];
     WhirlpoolDigest.C3        = new long[256];
     WhirlpoolDigest.C4        = new long[256];
     WhirlpoolDigest.C5        = new long[256];
     WhirlpoolDigest.C6        = new long[256];
     WhirlpoolDigest.C7        = new long[256];
     WhirlpoolDigest.EIGHT     = new short[32];
     WhirlpoolDigest.EIGHT[31] = 8;
     for (int i = 0; i < 256; i++)
     {
         int num  = WhirlpoolDigest.SBOX[i];
         int num2 = WhirlpoolDigest.maskWithReductionPolynomial(num << 1);
         int num3 = WhirlpoolDigest.maskWithReductionPolynomial(num2 << 1);
         int num4 = num3 ^ num;
         int num5 = WhirlpoolDigest.maskWithReductionPolynomial(num3 << 1);
         int num6 = num5 ^ num;
         WhirlpoolDigest.C0[i] = WhirlpoolDigest.packIntoLong(num, num, num3, num, num5, num4, num2, num6);
         WhirlpoolDigest.C1[i] = WhirlpoolDigest.packIntoLong(num6, num, num, num3, num, num5, num4, num2);
         WhirlpoolDigest.C2[i] = WhirlpoolDigest.packIntoLong(num2, num6, num, num, num3, num, num5, num4);
         WhirlpoolDigest.C3[i] = WhirlpoolDigest.packIntoLong(num4, num2, num6, num, num, num3, num, num5);
         WhirlpoolDigest.C4[i] = WhirlpoolDigest.packIntoLong(num5, num4, num2, num6, num, num, num3, num);
         WhirlpoolDigest.C5[i] = WhirlpoolDigest.packIntoLong(num, num5, num4, num2, num6, num, num, num3);
         WhirlpoolDigest.C6[i] = WhirlpoolDigest.packIntoLong(num3, num, num5, num4, num2, num6, num, num);
         WhirlpoolDigest.C7[i] = WhirlpoolDigest.packIntoLong(num, num3, num, num5, num4, num2, num6, num);
     }
 }
		private long[] _state = new long[8]; // the current "cipher" state



		/**
			* Copy constructor. This will copy the state of the provided message
			* digest.
			*/
		public WhirlpoolDigest(WhirlpoolDigest originalDigest)
		{
			Reset(originalDigest);
		}
		private long[] _state = new long[8]; // the current "cipher" state



		/**
			* Copy constructor. This will copy the state of the provided message
			* digest.
			*/
		public WhirlpoolDigest(WhirlpoolDigest originalDigest)
		{
			Array.Copy(originalDigest._rc, 0, _rc, 0, _rc.Length);

			Array.Copy(originalDigest._buffer, 0, _buffer, 0, _buffer.Length);

			this._bufferPos = originalDigest._bufferPos;
			Array.Copy(originalDigest._bitCount, 0, _bitCount, 0, _bitCount.Length);

			// -- internal hash state --
			Array.Copy(originalDigest._hash, 0, _hash, 0, _hash.Length);
			Array.Copy(originalDigest._K, 0, _K, 0, _K.Length);
			Array.Copy(originalDigest._L, 0, _L, 0, _L.Length);
			Array.Copy(originalDigest._block, 0, _block, 0, _block.Length);
			Array.Copy(originalDigest._state, 0, _state, 0, _state.Length);
		}
		private string createHexOutputFromDigest(byte[] digestBytes)
		{
			string resStr;
			IDigest digest = new WhirlpoolDigest();
			byte[] resBuf = new byte[digest.GetDigestSize()];
			digest.BlockUpdate(digestBytes, 0, digestBytes.Length);
			digest.DoFinal(resBuf, 0);
			resStr = Hex.ToHexString(resBuf);
			return resStr;
		}
Example #13
0
 /// <summary>
 /// Note:  Clears pool contents before returning
 /// </summary>
 private void CreateNewPRNG(byte[] pool)
 {
     if (pool == null)
     {
         throw new CryptographicException("Refusing to reseed with null pool");
     }
     try
     {
         if (pool.Length != PoolSize)
         {
             throw new CryptographicException("Refusing to reseed with invalid pool");
         }
         // Now, pool has been seeded, file operations are all completed, it's time to create my internal PRNG
         IDigest digest;
         switch (this.myRNGAlgorithm)
         {
             case PrngAlgorithm.MD5_128bit:
                 digest = new MD5Digest();
                 break;
             case PrngAlgorithm.RIPEMD128_128bit:
                 digest = new RipeMD128Digest();
                 break;
             case PrngAlgorithm.RIPEMD160_160bit:
                 digest = new RipeMD160Digest();
                 break;
             case PrngAlgorithm.RIPEMD256_256bit:
                 digest = new RipeMD256Digest();
                 break;
             case PrngAlgorithm.RIPEMD320_320bit:
                 digest = new RipeMD320Digest();
                 break;
             case PrngAlgorithm.SHA1_160bit:
                 digest = new Sha1Digest();
                 break;
             case PrngAlgorithm.SHA256_256bit:
                 digest = new Sha256Digest();
                 break;
             case PrngAlgorithm.SHA512_512bit:
                 digest = new Sha512Digest();
                 break;
             case PrngAlgorithm.Tiger_192bit:
                 digest = new TigerDigest();
                 break;
             case PrngAlgorithm.Whirlpool_512bit:
                 digest = new WhirlpoolDigest();
                 break;
             default:
                 throw new CryptographicException("Unknown prngAlgorithm specified: " + this.myRNGAlgorithm.ToString());
         }
         var drng = new DigestRandomGenerator(digest);
         drng.AddSeedMaterial(pool);
         this.myRNG = drng;
     }
     finally
     {
         Array.Clear(pool, 0, pool.Length);
     }
 }
Example #14
0
        private static void en_de_crypt(en_de_cryption_mode en_de_cryption_mode_current,
						string file_path_from,
						string file_path_to,
						bool delete_file_path_from,
						bool interactive)
        {
            Form form = new Form();

            Label password_1_label = new Label(),
                  password_2_label = new Label();

            int label_width = 150,
                label_height = 20,
                textbox_width = 200,
                textbox_height = 20,
                button_width = 80,
                button_height = 40;

            Color label_forecolor = Color.LightGreen,
                  password_backcolor = Color.Black,
                  password_forecolor = Color.LightGreen,
                  proceed_backcolor = Color.Black,
                  proceed_forecolor = Color.LightGreen,
                  cancel_backcolor = Color.Black,
                  cancel_forecolor = Color.LightGreen,
                  error_forecolor = Color.Red;

            ContentAlignment label_text_alignment = ContentAlignment.MiddleRight,
                     button_text_alignment = ContentAlignment.MiddleCenter;

            TextBox password_1 = new TextBox(),
                password_2 = new TextBox();

            char password_character = '*';

            BorderStyle password_border_style = BorderStyle.Fixed3D;

            Font label_font = new Font(FontFamily.GenericMonospace, (float)10.0),
                 password_font = new Font(FontFamily.GenericMonospace, (float)10.0),
                 button_font = new Font(FontFamily.GenericMonospace, (float)10.0);

            Button proceed = new Button(),
                   cancel = new Button();

            form.StartPosition = FormStartPosition.CenterParent;
            form.Width = 400;
            form.Height = 180;
            form.BackColor = Color.Black;
            form.Text = "Please, provide the password for the " +
                    (en_de_cryption_mode_current == en_de_cryption_mode.encrypt ?
                        "encryption" : "decryption") +
                    ".";

            password_1_label.Bounds = new Rectangle(10, 20, label_width, label_height);
            password_1_label.Font = label_font;
            password_1_label.ForeColor = label_forecolor;
            password_1_label.TextAlign = label_text_alignment;
            password_1_label.Text = "Password:"******"Confirm password:"******"OnClick",
                             BindingFlags.Instance |
                                 BindingFlags.NonPublic)
                          .Invoke(cancel, new object[] { null });

                if (en_de_cryption_mode_current == en_de_cryption_mode.encrypt) {
                    if (!string.IsNullOrEmpty(password_1.Text)) {
                        password_2.Enabled = true;
                    } else {
                        proceed.Enabled = false;
                        password_2.Text = "";
                        if (password_2.Enabled)
                            password_2.Enabled = false;
                    }
                } else {
                    if (!string.IsNullOrEmpty(password_1.Text))
                        proceed.Enabled = true;
                    else
                        proceed.Enabled = false;
                }

                if ((Keys)e.KeyValue == Keys.Enter &&
                    !string.IsNullOrEmpty(password_1.Text)) {
                    if (en_de_cryption_mode_current == en_de_cryption_mode.encrypt)
                        password_2.Focus();
                    else
                        proceed.GetType()
                               .GetMethod("OnClick",
                                      BindingFlags.Instance |
                                          BindingFlags.NonPublic)
                               .Invoke(proceed,
                                   new object[] { new KeyEventArgs(Keys.Enter) });
                }
            };

            password_2.Bounds = new Rectangle(160, 50, textbox_width, textbox_height);
            password_2.BackColor = password_backcolor;
            password_2.ForeColor = password_forecolor;
            password_2.BorderStyle = password_border_style;
            password_2.PasswordChar = password_character;
            password_2.Font = password_font;
            password_2.Multiline = false;
            password_2.Enabled = false;
            if (en_de_cryption_mode_current == en_de_cryption_mode.encrypt)
                password_2.KeyUp += (sender, e) =>
                {
                    if ((Keys)e.KeyValue == Keys.Escape)
                        cancel.GetType()
                              .GetMethod("OnClick",
                                 BindingFlags.Instance |
                                     BindingFlags.NonPublic)
                              .Invoke(cancel, new object[] { null });

                    if (string.IsNullOrEmpty(password_2.Text) ||
                        password_1.Text.Equals(password_2.Text)) {
                        if (!string.IsNullOrEmpty(password_2.Text))
                            proceed.Enabled = true;
                        password_2.ForeColor = password_forecolor;
                        if ((Keys)e.KeyValue == Keys.Enter &&
                            !string.IsNullOrEmpty(password_2.Text))
                            proceed.GetType()
                                   .GetMethod("OnClick",
                                      BindingFlags.Instance |
                                          BindingFlags.NonPublic)
                                   .Invoke(proceed,
                                       new object[] { new KeyEventArgs(Keys.Enter) });
                    } else {
                        proceed.Enabled = false;
                        password_2.ForeColor = error_forecolor;
                    }
                };

            proceed.Bounds = new Rectangle(290, 100, button_width, button_height);
            proceed.BackColor = proceed_backcolor;
            proceed.ForeColor = proceed_forecolor;
            proceed.Font = button_font;
            proceed.TextAlign = button_text_alignment;
            proceed.Text = "Proceed";
            proceed.Enabled = false;
            proceed.Click += (sender, e) =>
            {
                form.Close();

                byte[] key_string_bytes = Encoding.UTF8.GetBytes(password_1.Text), key = new byte[64];

                password_1.Text = "";
                password_2.Text = "";

                FileStream file_from = null, file_to = null;

                try {
                    WhirlpoolDigest whirlpool_digest = new WhirlpoolDigest();

                    whirlpool_digest.BlockUpdate(key_string_bytes, 0, key_string_bytes.Length);
                    whirlpool_digest.DoFinal(key, 0);

                    SerpentEngine serpent_engine = new SerpentEngine();

                    Pkcs7Padding pkcs7_padding = new Pkcs7Padding();
                    pkcs7_padding.Init(new SecureRandom(key));

                    PaddedBufferedBlockCipher
                        padded_buffered_block_cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(serpent_engine),
                                                             						     pkcs7_padding);

                    byte[] iv = new byte[padded_buffered_block_cipher.GetBlockSize()];
                    Array.Copy(key, key.Length - iv.Length, iv, 0, iv.Length);

                    padded_buffered_block_cipher.Init((en_de_cryption_mode_current == en_de_cryption_mode.encrypt),
                                      new ParametersWithIV(new KeyParameter(key, 31, 32),
                                                   iv));

                    int plaintext_buffer_size = 1024 * padded_buffered_block_cipher.GetBlockSize(),
                        ciphertext_buffer_size = padded_buffered_block_cipher.GetBlockSize() +
                                     plaintext_buffer_size,
                        bytes_read_count,
                        bytes_en_de_crypted_count;

                    byte[] buffer_from, buffer_to;

                    if (en_de_cryption_mode_current == en_de_cryption_mode.encrypt) {
                        buffer_from = new byte[plaintext_buffer_size];
                        buffer_to = new byte[ciphertext_buffer_size];
                    } else {
                        buffer_from = new byte[ciphertext_buffer_size];
                        buffer_to = new byte[plaintext_buffer_size];
                    }

                    file_from = new FileStream(file_path_from, FileMode.Open);
                    file_to = new FileStream(file_path_to, FileMode.Create);

                    while (true) {
                        bytes_read_count = file_from.Read(buffer_from, 0, buffer_from.Length);
                        if (bytes_read_count == 0) {
                            break;
                        }
                        bytes_en_de_crypted_count = padded_buffered_block_cipher.DoFinal(buffer_from,
                                                         0,
                                                         bytes_read_count,
                                                         buffer_to,
                                                         0);
                        file_to.Write(buffer_to, 0, bytes_en_de_crypted_count);
                    }

                    file_from.Close();
                    file_to.Close();

                    if (delete_file_path_from)
                        File.Delete(file_path_from);

                    for (int i = 0; i < key_string_bytes.Length; i += 1)
                        key_string_bytes[i] = 0;
                    for (int i = 0; i < key.Length; i += 1)
                        key[i] = 0;
                    whirlpool_digest.Reset();
                    serpent_engine.Reset();
                    pkcs7_padding = null;
                    padded_buffered_block_cipher.Reset();
                    for (int i = 0; i < iv.Length; i += 1)
                        iv[i] = 0;
                    for (int i = 0; i < buffer_from.Length; i += 1)
                        buffer_from[i] = 0;
                    for (int i = 0; i < buffer_to.Length; i += 1)
                        buffer_to[i] = 0;
                } catch(Exception exception) {
                    string message;
                    if (exception.Message.Contains("pad block corrupted")) {
                        if (file_from != null) {
                            file_from.Close();
                        }
                        if (file_to != null) {
                            file_to.Close();
                        }
                        try {
                            File.Delete(file_path_to);
                        } catch(Exception) {}
                        message = "A wrong decryption key has been provided.";
                    } else {
                        message = exception.Message;
                    }
                    alert(message, interactive);
                }
            };

            cancel.Bounds = new Rectangle(200, 100, button_width, button_height);
            cancel.BackColor = cancel_backcolor;
            cancel.ForeColor = cancel_forecolor;
            cancel.Font = button_font;
            cancel.TextAlign = button_text_alignment;
            cancel.Text = "Cancel";
            cancel.Click += (sender, e) =>
            {
                form.Close();

                password_1.Text = "";
                password_2.Text = "";

            };

            form.Controls.Add(password_1_label);
            form.Controls.Add(password_2_label);
            form.Controls.Add(password_1);
            form.Controls.Add(password_2);
            form.Controls.Add(proceed);
            form.Controls.Add(cancel);

            form.ShowDialog();
        }