Esempio n. 1
0
 private void fw(byte[] input)
 {
     Gost3411Digest.cpyBytesToShort(input, this.wS);
     this.w_S[15] = (this.wS[0] ^ this.wS[1] ^ this.wS[2] ^ this.wS[3] ^ this.wS[12] ^ this.wS[15]);
     Array.Copy(this.wS, 1, this.w_S, 0, 15);
     Gost3411Digest.cpyShortToBytes(this.w_S, input);
 }
Esempio n. 2
0
 public BigInteger GetDigest(byte[] message)
 {
     Gost3411Digest gost3411Digest = new Gost3411Digest();
     gost3411Digest.BlockUpdate(message, 0, message.Length);
     byte[] hashmessage = new byte[gost3411Digest.GetDigestSize()];
     gost3411Digest.DoFinal(hashmessage, 0);
     return new BigInteger(hashmessage);
 }
Esempio n. 3
0
        private static byte[] computeHash3411(byte[] input)
        {
            Gost3411Digest digest = new Gost3411Digest();

            byte[] output = new byte[GOST_3411_HASH_LENGTH];
            digest.BlockUpdate(input, 0, input.Length);
            digest.DoFinal(output, 0);

            return output;
        }
Esempio n. 4
0
        private static string computeHash3411(string input)
        {
            Gost3411Digest digest = new Gost3411Digest();

            byte[] output = new byte[GOST_3411_HASH_LENGTH];
            byte[] bInput = Utility.StringToByteArray(input);
            digest.BlockUpdate(bInput, 0, bInput.Length);
            digest.DoFinal(output, 0);

            return Utility.ByteArrayToString(output);
        }
Esempio n. 5
0
        public bool verifyPassword(string Password)
        {
            this.Password = Password;

            byte[] password = Utility.StringToByteArray(this.Password);
            Gost3411Digest digest = new Gost3411Digest();
            SecureRandom random = new SecureRandom();
            byte[] salt = random.GenerateSeed(16);
            digest.BlockUpdate(password, 0, password.Length);
            digest.BlockUpdate(salt, 0, 16);
            byte[] hash = new byte[digest.GetDigestSize()];
            digest.DoFinal(hash, 0);

            return true;
        }
Esempio n. 6
0
        public void Reset(IMemoable other)
        {
            Gost3411Digest gost3411Digest = (Gost3411Digest)other;

            sBox = gost3411Digest.sBox;
            cipher.Init(forEncryption: true, new ParametersWithSBox(null, sBox));
            Reset();
            global::System.Array.Copy((global::System.Array)gost3411Digest.H, 0, (global::System.Array)H, 0, gost3411Digest.H.Length);
            global::System.Array.Copy((global::System.Array)gost3411Digest.L, 0, (global::System.Array)L, 0, gost3411Digest.L.Length);
            global::System.Array.Copy((global::System.Array)gost3411Digest.M, 0, (global::System.Array)M, 0, gost3411Digest.M.Length);
            global::System.Array.Copy((global::System.Array)gost3411Digest.Sum, 0, (global::System.Array)Sum, 0, gost3411Digest.Sum.Length);
            global::System.Array.Copy((global::System.Array)gost3411Digest.C[1], 0, (global::System.Array)C[1], 0, gost3411Digest.C[1].Length);
            global::System.Array.Copy((global::System.Array)gost3411Digest.C[2], 0, (global::System.Array)C[2], 0, gost3411Digest.C[2].Length);
            global::System.Array.Copy((global::System.Array)gost3411Digest.C[3], 0, (global::System.Array)C[3], 0, gost3411Digest.C[3].Length);
            global::System.Array.Copy((global::System.Array)gost3411Digest.xBuf, 0, (global::System.Array)xBuf, 0, gost3411Digest.xBuf.Length);
            xBufOff   = gost3411Digest.xBufOff;
            byteCount = gost3411Digest.byteCount;
        }
Esempio n. 7
0
        public void Reset(IMemoable other)
        {
            Gost3411Digest gost3411Digest = (Gost3411Digest)other;

            this.sBox = gost3411Digest.sBox;
            this.cipher.Init(true, new ParametersWithSBox(null, this.sBox));
            this.Reset();
            Array.Copy(gost3411Digest.H, 0, this.H, 0, gost3411Digest.H.Length);
            Array.Copy(gost3411Digest.L, 0, this.L, 0, gost3411Digest.L.Length);
            Array.Copy(gost3411Digest.M, 0, this.M, 0, gost3411Digest.M.Length);
            Array.Copy(gost3411Digest.Sum, 0, this.Sum, 0, gost3411Digest.Sum.Length);
            Array.Copy(gost3411Digest.C[1], 0, this.C[1], 0, gost3411Digest.C[1].Length);
            Array.Copy(gost3411Digest.C[2], 0, this.C[2], 0, gost3411Digest.C[2].Length);
            Array.Copy(gost3411Digest.C[3], 0, this.C[3], 0, gost3411Digest.C[3].Length);
            Array.Copy(gost3411Digest.xBuf, 0, this.xBuf, 0, gost3411Digest.xBuf.Length);
            this.xBufOff   = gost3411Digest.xBufOff;
            this.byteCount = gost3411Digest.byteCount;
        }
		/**
		 * Copy constructor.  This will copy the state of the provided
		 * message digest.
		 */
		public Gost3411Digest(Gost3411Digest t)
		{
			this.sBox = t.sBox;
			cipher.Init(true, new ParametersWithSBox(null, sBox));

			Reset();

			Array.Copy(t.H, 0, this.H, 0, t.H.Length);
			Array.Copy(t.L, 0, this.L, 0, t.L.Length);
			Array.Copy(t.M, 0, this.M, 0, t.M.Length);
			Array.Copy(t.Sum, 0, this.Sum, 0, t.Sum.Length);
			Array.Copy(t.C[1], 0, this.C[1], 0, t.C[1].Length);
			Array.Copy(t.C[2], 0, this.C[2], 0, t.C[2].Length);
			Array.Copy(t.C[3], 0, this.C[3], 0, t.C[3].Length);
			Array.Copy(t.xBuf, 0, this.xBuf, 0, t.xBuf.Length);

			this.xBufOff = t.xBufOff;
			this.byteCount = t.byteCount;
		}
Esempio n. 9
0
 public Gost3411Digest(Gost3411Digest t)
 {
     this.H      = new byte[0x20];
     this.L      = new byte[0x20];
     this.M      = new byte[0x20];
     this.Sum    = new byte[0x20];
     this.C      = MakeC();
     this.xBuf   = new byte[0x20];
     this.cipher = new Gost28147Engine();
     this.K      = new byte[0x20];
     this.a      = new byte[8];
     this.wS     = new short[0x10];
     this.w_S    = new short[0x10];
     this.S      = new byte[0x20];
     this.U      = new byte[0x20];
     this.V      = new byte[0x20];
     this.W      = new byte[0x20];
     this.Reset(t);
 }
Esempio n. 10
0
        /**
         * Copy constructor.  This will copy the state of the provided
         * message digest.
         */
        public Gost3411Digest(Gost3411Digest t)
            : this()
        {
//			cipher.Init(true, new ParametersWithSBox(null, Gost28147Engine.GetSBox("D-A")));
//
//			Reset();

            Array.Copy(t.H, 0, this.H, 0, t.H.Length);
            Array.Copy(t.L, 0, this.L, 0, t.L.Length);
            Array.Copy(t.M, 0, this.M, 0, t.M.Length);
            Array.Copy(t.Sum, 0, this.Sum, 0, t.Sum.Length);
            Array.Copy(t.C[1], 0, this.C[1], 0, t.C[1].Length);
            Array.Copy(t.C[2], 0, this.C[2], 0, t.C[2].Length);
            Array.Copy(t.C[3], 0, this.C[3], 0, t.C[3].Length);
            Array.Copy(t.xBuf, 0, this.xBuf, 0, t.xBuf.Length);

            this.xBufOff   = t.xBufOff;
            this.byteCount = t.byteCount;
        }
Esempio n. 11
0
		/**
		* Copy constructor.  This will copy the state of the provided
		* message digest.
		*/
		public Gost3411Digest(Gost3411Digest t)
			: this()
		{
//			cipher.Init(true, new ParametersWithSBox(null, Gost28147Engine.GetSBox("D-A")));
//
//			Reset();

			Array.Copy(t.H, 0, this.H, 0, t.H.Length);
			Array.Copy(t.L, 0, this.L, 0, t.L.Length);
			Array.Copy(t.M, 0, this.M, 0, t.M.Length);
			Array.Copy(t.Sum, 0, this.Sum, 0, t.Sum.Length);
			Array.Copy(t.C[1], 0, this.C[1], 0, t.C[1].Length);
			Array.Copy(t.C[2], 0, this.C[2], 0, t.C[2].Length);
			Array.Copy(t.C[3], 0, this.C[3], 0, t.C[3].Length);
			Array.Copy(t.xBuf, 0, this.xBuf, 0, t.xBuf.Length);

			this.xBufOff = t.xBufOff;
			this.byteCount = t.byteCount;
		}
Esempio n. 12
0
        /**
         * Copy constructor.  This will copy the state of the provided
         * message digest.
         */
        public Gost3411Digest(Gost3411Digest t)
        {
            this.sBox = t.sBox;
            cipher.Init(true, new ParametersWithSBox(null, sBox));

            Reset();

            Array.Copy(t.H, 0, this.H, 0, t.H.Length);
            Array.Copy(t.L, 0, this.L, 0, t.L.Length);
            Array.Copy(t.M, 0, this.M, 0, t.M.Length);
            Array.Copy(t.Sum, 0, this.Sum, 0, t.Sum.Length);
            Array.Copy(t.C[1], 0, this.C[1], 0, t.C[1].Length);
            Array.Copy(t.C[2], 0, this.C[2], 0, t.C[2].Length);
            Array.Copy(t.C[3], 0, this.C[3], 0, t.C[3].Length);
            Array.Copy(t.xBuf, 0, this.xBuf, 0, t.xBuf.Length);

            this.xBufOff   = t.xBufOff;
            this.byteCount = t.byteCount;
        }
Esempio n. 13
0
		private byte[] generateKey(byte[] startkey)
		{
			byte[] newKey = new byte[Gost28147_KEY_LENGTH];

			Gost3411Digest digest = new Gost3411Digest();

			digest.BlockUpdate(startkey, 0, startkey.Length);
			digest.DoFinal(newKey, 0);

			return newKey;
		}
Esempio n. 14
0
		/**
		 * Copy constructor.  This will copy the state of the provided
		 * message digest.
		 */
		public Gost3411Digest(Gost3411Digest t)
		{
			Reset(t);
		}
Esempio n. 15
0
 /**
  * Copy constructor.  This will copy the state of the provided
  * message digest.
  */
 public Gost3411Digest(Gost3411Digest t)
 {
     Reset(t);
 }
		private byte[] SignGost(byte[] buffer, int length)
		{
			ECGost3410Signer signer = new ECGost3410Signer();
			signer.Init(true, new ParametersWithRandom(PrivateKeyFactory.CreateKey(PrivateKey), _secureRandom));

			var digest = new Gost3411Digest();

			digest.BlockUpdate(buffer, 0, length);
			byte[] hash = new byte[digest.GetDigestSize()];
			digest.DoFinal(hash, 0);

			var signature = signer.GenerateSignature(hash);

			byte[] res = new byte[64];

			signature[0].ToByteArrayUnsigned().CopyTo(res, 32);
			signature[1].ToByteArrayUnsigned().CopyTo(res, 0);

			return res;
		}
		private bool VerifyGost(byte[] buffer, int length, byte[] signature)
		{
			ECDomainParameters dParams = ECGost3410NamedCurves.GetByOid(CryptoProObjectIdentifiers.GostR3410x2001CryptoProA);
			byte[] reversedPublicKey = PublicKey.Reverse().ToArray();
			ECPoint q = dParams.Curve.CreatePoint(new BigInteger(1, reversedPublicKey, 32, 32), new BigInteger(1, reversedPublicKey, 0, 32), false);
			ECPublicKeyParameters parameters = new ECPublicKeyParameters(q, dParams);

			var signer = new ECGost3410Signer();
			signer.Init(false, parameters);

			var digest = new Gost3411Digest();

			digest.BlockUpdate(buffer, 0, length);
			byte[] hash = new byte[digest.GetDigestSize()];
			digest.DoFinal(hash, 0);

			return signer.VerifySignature(hash, new BigInteger(1, signature, 32, 32), new BigInteger(1, signature, 0, 32));
		}
Esempio n. 18
0
 public Gost3411Digest(Gost3411Digest t)
 {
     this.Reset(t);
 }
Esempio n. 19
0
        /**
         * Test Sign and Verify with test parameters
         * see: http://www.ietf.org/internet-drafts/draft-popov-cryptopro-cpalgs-01.txt
         * gostR3410-2001-TestParamSet  P.46
         */
        private void ecGOST3410_TestParam()
        {
            SecureRandom    random = new SecureRandom();

            BigInteger mod_p = new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564821041"); //p

            FpCurve curve = new FpCurve(
                mod_p, // p
                new BigInteger("7"), // a
                new BigInteger("43308876546767276905765904595650931995942111794451039583252968842033849580414")); // b

            ECDomainParameters parameters = new ECDomainParameters(
                                   curve,
                                   new FpPoint(curve,
                                   new FpFieldElement(mod_p,new BigInteger("2")), // x
                                   new FpFieldElement(mod_p,new BigInteger("4018974056539037503335449422937059775635739389905545080690979365213431566280"))), // y
                                   new BigInteger("57896044618658097711785492504343953927082934583725450622380973592137631069619")); // q

            ECKeyPairGenerator          pGen = new ECKeyPairGenerator();
            ECKeyGenerationParameters   genParam = new ECKeyGenerationParameters(
                parameters,
                random);

            pGen.Init(genParam);

            AsymmetricCipherKeyPair  pair = pGen.GenerateKeyPair();

            ParametersWithRandom param = new ParametersWithRandom(pair.Private, random);

            ECGost3410Signer ecgost3410 = new ECGost3410Signer();

            ecgost3410.Init(true, param);

            //get hash message using the digest GOST3411.
            byte[] message = Encoding.ASCII.GetBytes("Message for sign");
            Gost3411Digest gost3411 = new Gost3411Digest();
            gost3411.BlockUpdate(message, 0, message.Length);
            byte[] hashmessage = new byte[gost3411.GetDigestSize()];
            gost3411.DoFinal(hashmessage, 0);

            BigInteger[] sig = ecgost3410.GenerateSignature(hashmessage);

            ecgost3410.Init(false, pair.Public);

            if (!ecgost3410.VerifySignature(hashmessage, sig[0], sig[1]))
            {
                Fail("signature fails");
            }
        }
Esempio n. 20
0
        public LavaResult AddNewUser(LavaUser user)
        {
            LavaResult result = new LavaResult();

            SqlConnection conn = new SqlConnection(connectionString);

            SqlCommand cmdNewCustomer = new SqlCommand("Volcano.uspNewUser", conn);
            cmdNewCustomer.CommandType = CommandType.StoredProcedure;

            cmdNewCustomer.Parameters.Add(new SqlParameter("@UserName", SqlDbType.NChar, 32));
            cmdNewCustomer.Parameters["@UserName"].Value = user.UserName;

            byte[] password = Utility.StringToByteArray(user.Password);
            Gost3411Digest digest = new Gost3411Digest();
            SecureRandom random = new SecureRandom();
            byte[] salt = random.GenerateSeed(16);
            digest.BlockUpdate(password, 0, password.Length);
            digest.BlockUpdate(salt, 0, 16);
            byte[] hash = new byte[digest.GetDigestSize()];
            digest.DoFinal(hash, 0);

            cmdNewCustomer.Parameters.AddWithValue("@Password", hash);
            cmdNewCustomer.Parameters.AddWithValue("@Salt", salt);

            cmdNewCustomer.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Int));
            cmdNewCustomer.Parameters["@UserID"].Direction = ParameterDirection.Output;

            try
            {
                conn.Open();
                cmdNewCustomer.ExecuteNonQuery();
                user.UserID = (int)cmdNewCustomer.Parameters["@UserID"].Value;
            }
            catch (SqlException sqlEx)
            {
                if (sqlEx.Errors.Count > 0) // Assume the interesting stuff is in the first error
                {
                    switch (sqlEx.Errors[0].Number)
                    {
                        case 2627: // Foreign Key violation
                            result.Result = LAVA_ERROR_CODE.USER_ALREADY_EXIST;
                            result.Message = "UserName already exist. " + sqlEx.Message;
                            break;
                        default:
                            result.Result = LAVA_ERROR_CODE.UNKNOWH_ERROR;
                            result.Message = "Customer ID was not returned. Account could not be created. " + sqlEx.Errors[0].Number + sqlEx.Message;
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Result = LAVA_ERROR_CODE.UNKNOWH_ERROR;
                result.Message = "Customer ID was not returned. Account could not be created. " + ex.Message;
            }
            finally
            {
                conn.Close();
            }

            return result;
        }