Example #1
0
        /**
         * Decode a point on this curve from its ASN.1 encoding. The different
         * encodings are taken account of, including point compression for
         * <code>F<sub>p</sub></code> (X9.62 s 4.2.1 pg 17).
         * @return The decoded point.
         */
        public virtual ECPoint DecodePoint(byte[] encoded)
        {
            ECPoint p = null;
            int     expectedLength = (FieldSize + 7) / 8;

            byte type = encoded[0];

            switch (type)
            {
            case 0x00:     // infinity
            {
                if (encoded.Length != 1)
                {
                    throw new ArgumentException("Incorrect length for infinity encoding", "encoded");
                }

                p = Infinity;
                break;
            }

            case 0x02:     // compressed
            case 0x03:     // compressed
            {
                if (encoded.Length != (expectedLength + 1))
                {
                    throw new ArgumentException("Incorrect length for compressed encoding", "encoded");
                }

                int        yTilde = type & 1;
                BigInteger X      = new BigInteger(1, encoded, 1, expectedLength);

                p = DecompressPoint(yTilde, X);
                if (!p.SatisfiesCofactor())
                {
                    throw new ArgumentException("Invalid point");
                }

                break;
            }

            case 0x04:     // uncompressed
            {
                if (encoded.Length != (2 * expectedLength + 1))
                {
                    throw new ArgumentException("Incorrect length for uncompressed encoding", "encoded");
                }

                BigInteger X = new BigInteger(1, encoded, 1, expectedLength);
                BigInteger Y = new BigInteger(1, encoded, 1 + expectedLength, expectedLength);

                p = ValidatePoint(X, Y);
                break;
            }

            case 0x06:     // hybrid
            case 0x07:     // hybrid
            {
                if (encoded.Length != (2 * expectedLength + 1))
                {
                    throw new ArgumentException("Incorrect length for hybrid encoding", "encoded");
                }

                BigInteger X = new BigInteger(1, encoded, 1, expectedLength);
                BigInteger Y = new BigInteger(1, encoded, 1 + expectedLength, expectedLength);

                if (Y.TestBit(0) != (type == 0x07))
                {
                    throw new ArgumentException("Inconsistent Y coordinate in hybrid encoding", "encoded");
                }

                p = ValidatePoint(X, Y);
                break;
            }

            default:
                throw new FormatException("Invalid point encoding " + type);
            }

            if (type != 0x00 && p.IsInfinity)
            {
                throw new ArgumentException("Invalid infinity encoding", "encoded");
            }

            return(p);
        }
Example #2
0
        public virtual ECPoint DecodePoint(byte[] encoded)
        {
            //IL_0050: Unknown result type (might be due to invalid IL or missing references)
            //IL_0074: Unknown result type (might be due to invalid IL or missing references)
            //IL_00a3: Unknown result type (might be due to invalid IL or missing references)
            //IL_00bd: Unknown result type (might be due to invalid IL or missing references)
            //IL_00fc: Unknown result type (might be due to invalid IL or missing references)
            //IL_0132: Unknown result type (might be due to invalid IL or missing references)
            //IL_0155: Unknown result type (might be due to invalid IL or missing references)
            //IL_0170: Unknown result type (might be due to invalid IL or missing references)
            ECPoint eCPoint = null;
            int     num     = (FieldSize + 7) / 8;
            byte    b       = encoded[0];

            switch (b)
            {
            case 0:
                if (encoded.Length != 1)
                {
                    throw new ArgumentException("Incorrect length for infinity encoding", "encoded");
                }
                eCPoint = Infinity;
                break;

            case 2:
            case 3:
            {
                if (encoded.Length != num + 1)
                {
                    throw new ArgumentException("Incorrect length for compressed encoding", "encoded");
                }
                int        yTilde = b & 1;
                BigInteger x3     = new BigInteger(1, encoded, 1, num);
                eCPoint = DecompressPoint(yTilde, x3);
                if (!eCPoint.SatisfiesCofactor())
                {
                    throw new ArgumentException("Invalid point");
                }
                break;
            }

            case 4:
            {
                if (encoded.Length != 2 * num + 1)
                {
                    throw new ArgumentException("Incorrect length for uncompressed encoding", "encoded");
                }
                BigInteger x2 = new BigInteger(1, encoded, 1, num);
                BigInteger y  = new BigInteger(1, encoded, 1 + num, num);
                eCPoint = ValidatePoint(x2, y);
                break;
            }

            case 6:
            case 7:
            {
                if (encoded.Length != 2 * num + 1)
                {
                    throw new ArgumentException("Incorrect length for hybrid encoding", "encoded");
                }
                BigInteger x          = new BigInteger(1, encoded, 1, num);
                BigInteger bigInteger = new BigInteger(1, encoded, 1 + num, num);
                if (bigInteger.TestBit(0) != (b == 7))
                {
                    throw new ArgumentException("Inconsistent Y coordinate in hybrid encoding", "encoded");
                }
                eCPoint = ValidatePoint(x, bigInteger);
                break;
            }

            default:
                throw new FormatException(string.Concat((object)"Invalid point encoding ", (object)b));
            }
            if (b != 0 && eCPoint.IsInfinity)
            {
                throw new ArgumentException("Invalid infinity encoding", "encoded");
            }
            return(eCPoint);
        }
Example #3
0
        public virtual ECPoint DecodePoint(byte[] encoded)
        {
            ECPoint eCPoint = null;
            int     num     = (FieldSize + 7) / 8;
            byte    b       = encoded[0];

            switch (b)
            {
            case 0:
                if (encoded.Length != 1)
                {
                    throw new ArgumentException("Incorrect length for infinity encoding", "encoded");
                }
                eCPoint = Infinity;
                break;

            case 2:
            case 3:
            {
                if (encoded.Length != num + 1)
                {
                    throw new ArgumentException("Incorrect length for compressed encoding", "encoded");
                }
                int        yTilde = b & 1;
                BigInteger x3     = new BigInteger(1, encoded, 1, num);
                eCPoint = DecompressPoint(yTilde, x3);
                if (!eCPoint.SatisfiesCofactor())
                {
                    throw new ArgumentException("Invalid point");
                }
                break;
            }

            case 4:
            {
                if (encoded.Length != 2 * num + 1)
                {
                    throw new ArgumentException("Incorrect length for uncompressed encoding", "encoded");
                }
                BigInteger x2 = new BigInteger(1, encoded, 1, num);
                BigInteger y  = new BigInteger(1, encoded, 1 + num, num);
                eCPoint = ValidatePoint(x2, y);
                break;
            }

            case 6:
            case 7:
            {
                if (encoded.Length != 2 * num + 1)
                {
                    throw new ArgumentException("Incorrect length for hybrid encoding", "encoded");
                }
                BigInteger x          = new BigInteger(1, encoded, 1, num);
                BigInteger bigInteger = new BigInteger(1, encoded, 1 + num, num);
                if (bigInteger.TestBit(0) != (b == 7))
                {
                    throw new ArgumentException("Inconsistent Y coordinate in hybrid encoding", "encoded");
                }
                eCPoint = ValidatePoint(x, bigInteger);
                break;
            }

            default:
                throw new FormatException("Invalid point encoding " + b);
            }
            if (b != 0 && eCPoint.IsInfinity)
            {
                throw new ArgumentException("Invalid infinity encoding", "encoded");
            }
            return(eCPoint);
        }
Example #4
0
        public virtual ECPoint DecodePoint(byte[] encoded)
        {
            ECPoint infinity = null;
            int     length   = (this.FieldSize + 7) / 8;
            byte    num2     = encoded[0];

            switch (num2)
            {
            case 0:
                if (encoded.Length != 1)
                {
                    throw new ArgumentException("Incorrect length for infinity encoding", "encoded");
                }
                infinity = this.Infinity;
                break;

            case 2:
            case 3:
            {
                if (encoded.Length != (length + 1))
                {
                    throw new ArgumentException("Incorrect length for compressed encoding", "encoded");
                }
                int        yTilde  = num2 & 1;
                BigInteger integer = new BigInteger(1, encoded, 1, length);
                infinity = this.DecompressPoint(yTilde, integer);
                if (!infinity.SatisfiesCofactor())
                {
                    throw new ArgumentException("Invalid point");
                }
                break;
            }

            case 4:
            {
                if (encoded.Length != ((2 * length) + 1))
                {
                    throw new ArgumentException("Incorrect length for uncompressed encoding", "encoded");
                }
                BigInteger x = new BigInteger(1, encoded, 1, length);
                BigInteger y = new BigInteger(1, encoded, 1 + length, length);
                infinity = this.ValidatePoint(x, y);
                break;
            }

            case 6:
            case 7:
            {
                if (encoded.Length != ((2 * length) + 1))
                {
                    throw new ArgumentException("Incorrect length for hybrid encoding", "encoded");
                }
                BigInteger x = new BigInteger(1, encoded, 1, length);
                BigInteger y = new BigInteger(1, encoded, 1 + length, length);
                if (y.TestBit(0) != (num2 == 7))
                {
                    throw new ArgumentException("Inconsistent Y coordinate in hybrid encoding", "encoded");
                }
                infinity = this.ValidatePoint(x, y);
                break;
            }

            default:
                throw new FormatException("Invalid point encoding " + num2);
            }
            if ((num2 != 0) && infinity.IsInfinity)
            {
                throw new ArgumentException("Invalid infinity encoding", "encoded");
            }
            return(infinity);
        }