Exemplo n.º 1
0
        public void TestVerify()
        {
            var input     = Encoding.UTF8.GetBytes("Hello!");
            var signature = HexConverter.FromHex("3046022100cff2771c25049757d8a14e6f9a58b7e0928bafd33d7977fd194aeb14c64bd6ab022100f417bdbc4a9cb1f5cd719583b093a767c1c975e7ac5604ddd47827e2da05e8bb");

            var ecdsa = new ECDSA(new DefaultRandomGenerator());

            ecdsa.Init(new ECCipherParameters(Secp256R1.Parameters, PublicKey));

            var result = ecdsa.Verify(input, signature, new SHA256Digest(SHA256Digest.Mode.SHA256));

            Assert.True(result);
        }
Exemplo n.º 2
0
        public void TestVerify()
        {
            var input     = Encoding.UTF8.GetBytes("Hello!");
            var signature = HexConverter.FromHex("304602210098a1615b14266bb514f3829f2775e1a46eec972c1021d67dd1c35b88add5e3f6022100eb91ddd49f9ab3560d69d65b47961fb051ef72c18c3c9acd7f2fb4d1c37ce351");

            var ecdsa = new ECDSA(new DefaultRandomGenerator());

            ecdsa.Init(new ECCipherParameters(Secp256K1.Parameters, PublicKey));

            var result = ecdsa.Verify(input, signature, new SHA256Digest(SHA256Digest.Mode.SHA256));

            Assert.True(result);
        }
Exemplo n.º 3
0
        public void TestSignature()
        {
            var input = Encoding.UTF8.GetBytes("Hello!");

            var ecdsa = new ECDSA(new DefaultRandomGenerator());

            ecdsa.Init(new ECCipherParameters(Secp256R1.Parameters, PrivateKey));

            var signature = ecdsa.Sign(input, new SHA256Digest(SHA256Digest.Mode.SHA256));
            var result    = ecdsa.Verify(input, signature, new SHA256Digest(SHA256Digest.Mode.SHA256));

            Assert.True(result);
        }
Exemplo n.º 4
0
        byte[] ParseServerKeyExchange(out ECCurve curve, IPublicKey pkey)
        {
            byte[] msg = ReadHandshakeMessageExpected(
                SSL.SERVER_KEY_EXCHANGE);
            if (msg.Length < 4)
            {
                throw new SSLException(
                          "Invalid ServerKeyExchange message");
            }
            if (msg[0] != 0x03)
            {
                throw new SSLException("Unsupported unnamed curve");
            }
            curve = SSL.GetCurveByID(IO.Dec16be(msg, 1));
            int plen = msg[3];
            int off  = 4;

            if (msg.Length - off < plen)
            {
                throw new SSLException(
                          "Invalid ServerKeyExchange message");
            }
            byte[] point = new byte[plen];
            Array.Copy(msg, off, point, 0, plen);
            off += plen;
            int slen = off;

            int hashId, sigId;

            if (Version >= SSL.TLS12)
            {
                if (msg.Length - off < 2)
                {
                    throw new SSLException(
                              "Invalid ServerKeyExchange message");
                }
                hashId = msg[off++];
                if (hashId == 0)
                {
                    throw new SSLException(
                              "Invalid hash identifier");
                }
                sigId = msg[off++];
            }
            else
            {
                if (pkey is RSAPublicKey)
                {
                    hashId = 0;
                    sigId  = 1;
                }
                else if (pkey is ECPublicKey)
                {
                    hashId = 2;
                    sigId  = 3;
                }
                else
                {
                    throw new SSLException(
                              "Unsupported signature key type");
                }
            }

            if (msg.Length - off < 2)
            {
                throw new SSLException(
                          "Invalid ServerKeyExchange message");
            }
            int sigLen = IO.Dec16be(msg, off);

            off += 2;
            if (sigLen != msg.Length - off)
            {
                throw new SSLException(
                          "Invalid ServerKeyExchange message");
            }
            byte[] sig = new byte[sigLen];
            Array.Copy(msg, off, sig, 0, sigLen);

            byte[] hv;
            if (hashId == 0)
            {
                MD5  md5  = new MD5();
                SHA1 sha1 = new SHA1();
                md5.Update(clientRandom);
                md5.Update(serverRandom);
                md5.Update(msg, 0, slen);
                sha1.Update(clientRandom);
                sha1.Update(serverRandom);
                sha1.Update(msg, 0, slen);
                hv = new byte[36];
                md5.DoFinal(hv, 0);
                sha1.DoFinal(hv, 16);
            }
            else
            {
                IDigest h = SSL.GetHashByID(hashId);
                h.Update(clientRandom);
                h.Update(serverRandom);
                h.Update(msg, 0, slen);
                hv = h.DoFinal();
            }

            bool ok;

            if (sigId == 1)
            {
                RSAPublicKey rpk = pkey as RSAPublicKey;
                if (rpk == null)
                {
                    throw new SSLException(
                              "Wrong public key type for RSA");
                }
                if (hashId == 0)
                {
                    ok = RSA.VerifyND(rpk, hv, sig);
                }
                else
                {
                    byte[] head1, head2;

                    switch (hashId)
                    {
                    case 1:
                        head1 = RSA.PKCS1_MD5;
                        head2 = RSA.PKCS1_MD5_ALT;
                        break;

                    case 2:
                        head1 = RSA.PKCS1_SHA1;
                        head2 = RSA.PKCS1_SHA1_ALT;
                        break;

                    case 3:
                        head1 = RSA.PKCS1_SHA224;
                        head2 = RSA.PKCS1_SHA224_ALT;
                        break;

                    case 4:
                        head1 = RSA.PKCS1_SHA256;
                        head2 = RSA.PKCS1_SHA256_ALT;
                        break;

                    case 5:
                        head1 = RSA.PKCS1_SHA384;
                        head2 = RSA.PKCS1_SHA384_ALT;
                        break;

                    case 6:
                        head1 = RSA.PKCS1_SHA512;
                        head2 = RSA.PKCS1_SHA512_ALT;
                        break;

                    default:
                        throw new SSLException(
                                  "Unsupported hash algorithm: "
                                  + hashId);
                    }
                    ok = RSA.Verify(rpk, head1, head2, hv, sig);
                }
            }
            else if (sigId == 3)
            {
                ECPublicKey epk = pkey as ECPublicKey;
                if (epk == null)
                {
                    throw new SSLException(
                              "Wrong public key type for ECDSA");
                }
                ok = ECDSA.Verify(epk, hv, sig);
            }
            else
            {
                throw new SSLException(
                          "Unsupported signature type: " + sigId);
            }

            if (!ok)
            {
                throw new SSLException(
                          "Invalid signature on ServerKeyExchange");
            }
            return(point);
        }
Exemplo n.º 5
0
        public PUnite()
        {
            Codes = new List <Commands>();
            _one  = new byte[64];
            for (int i = 0; i < 64; i++)
            {
                _one[i] = 1;
            }
            _zero = new byte[64];
            for (int i = 0; i < 64; i++)
            {
                _zero[i] = 0;
            }
            _stack   = new Stack();
            _actions = new Dictionary <Commands, Func <bool?> >();
            _actions.Add(Commands.Zero, () =>
            {
                _stack.Push(_zero);
                return(null);
            });
            _actions.Add(Commands.One, () =>
            {
                _stack.Push(_one);
                return(null);
            });
            _actions.Add(Commands.MD5, () =>
            {
                _stack.Pop(out string data);
                var data2 = data.ComputeHash(HashAlgorithms.MD5);
                //var data3 = new byte[64];
                //for (int i = 0; i < data2.Length; i++)
                //{
                //    data3[i] = data2[i];
                //}
                _stack.Push(data2);
                return(null);
            });
            _actions.Add(Commands.SHA256, () =>
            {
                _stack.Pop(out string data);
                var data2 = data.ComputeHash(HashAlgorithms.SHA256);
                //var data3 = new byte[64];
                //for (int i = 0; i < data2.Length; i++)
                //{
                //    data3[i] = data2[i];
                //}
                _stack.Push(data2);
                return(null);
            });
            _actions.Add(Commands.DoubleSHA256, () =>
            {
                _stack.Pop(out string data);
                var data2 = data.ComputeHash(HashAlgorithms.DoubleSHA256);
                //var data3 = new byte[64];
                //for (int i = 0; i < data2.Length; i++)
                //{
                //    data3[i] = data2[i];
                //}
                _stack.Push(data2);
                return(null);
            });
            _actions.Add(Commands.SHA512, () =>
            {
                _stack.Pop(out string data);
                var data2 = data.ComputeHash(HashAlgorithms.SHA512);
                _stack.Push(data2);
                return(null);
            });
            _actions.Add(Commands.DoubleSHA512, () =>
            {
                _stack.Pop(out string data);
                var data2 = data.ComputeHash(HashAlgorithms.DoubleSHA512);
                _stack.Push(data2);
                return(null);
            });

            _actions.Add(Commands.Dup, () =>
            {
                _stack.Pop(out string data);
                _stack.Push(data);
                return(null);
            });
            _actions.Add(Commands.CheckSig, () =>
            {
                _stack.Pop(out string PubKey);
                _stack.Pop(out string Sig);
                _stack.Pop(out string Message);
                //var MessageHash = Message.ComputeHashString(HashAlgorithms.DoubleSHA512);
                var ecdsa = new ECDSA(PubKey);
                var res   = ecdsa.Verify(Sig, Message);
                if (res)
                {
                    _stack.Push(_one);
                }
                else
                {
                    _stack.Push(_zero);
                }
                return(null);
            });
            _actions.Add(Commands.IsOne, () =>
            {
                var data = _stack.Peek;
                var res  = true;
                foreach (var item in data)
                {
                    if (item != 1)
                    {
                        res = false;
                        break;
                    }
                }
                return(res);
            });
            _actions.Add(Commands.IsZero, () =>
            {
                var data = _stack.Peek;
                var res  = true;
                foreach (var item in data)
                {
                    if (item != 0)
                    {
                        res = false;
                        break;
                    }
                }
                return(res);
            });
            _actions.Add(Commands.Eq, () =>
            {
                _stack.Pop(out string data1);
                _stack.Pop(out string data2);
                var res = data1.Equals(data2);
                if (res)
                {
                    _stack.Push(_one);
                }
                else
                {
                    _stack.Push(_zero);
                }
                return(null);
            });
        }