You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ml...@apache.org on 2006/07/21 12:04:49 UTC

svn commit: r424250 - in /incubator/harmony/enhanced/classlib/trunk/modules/security/src: main/java/common/java/security/spec/EllipticCurve.java test/api/java/org/apache/harmony/security/tests/java/security/spec/EllipticCurveTest.java

Author: mloenko
Date: Fri Jul 21 03:04:47 2006
New Revision: 424250

URL: http://svn.apache.org/viewvc?rev=424250&view=rev
Log:
fixes for HARMONY-731
[classlib][security]compatibility: unexpected IllegalArgumentException for java.security.spec.EllipticCurve(<2>, 4, 1)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/spec/EllipticCurve.java
    incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/spec/EllipticCurveTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/spec/EllipticCurve.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/spec/EllipticCurve.java?rev=424250&r1=424249&r2=424250&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/spec/EllipticCurve.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/spec/EllipticCurve.java Fri Jul 21 03:04:47 2006
@@ -29,23 +29,27 @@
  * 
  */
 public class EllipticCurve {
+
     // Underlying finite field
     private final ECField field;
+
     // The first coefficient of the equation defining this elliptic curve
     private final BigInteger a;
+
     // The second coefficient of the equation defining this elliptic curve
-   private final BigInteger b;
+    private final BigInteger b;
+
     // Bytes used during this elliptic curve generation,
     // if it was generated randomly
     private final byte[] seed;
+
     // Hash code
     private volatile int hash;
 
     /**
      * @com.intel.drl.spec_ref
      */
-    public EllipticCurve(ECField field, BigInteger a,
-            BigInteger b, byte[] seed) {
+    public EllipticCurve(ECField field, BigInteger a, BigInteger b, byte[] seed) {
         this.field = field;
         if (this.field == null) {
             throw new NullPointerException("the field parameter is null");
@@ -65,20 +69,18 @@
             this.seed = new byte[seed.length];
             System.arraycopy(seed, 0, this.seed, 0, this.seed.length);
         }
-        // check parameters
+        // check parameters for ECFieldFp and ECFieldF2m.
         // Check invariant: a and b must be in the field.
+        // Check conditions for custom ECField are not specified.
         if (this.field instanceof ECFieldFp) {
-            BigInteger p = ((ECFieldFp)this.field).getP();
-            if (this.a.signum() < 0 ||
-                this.a.compareTo(p) >= 0) {
+            BigInteger p = ((ECFieldFp) this.field).getP();
+            if (this.a.signum() < 0 || this.a.compareTo(p) >= 0) {
                 throw new IllegalArgumentException("the a is not in the field");
             }
-            if (this.b.signum() < 0 ||
-                    this.b.compareTo(p) >= 0) {
-                    throw new IllegalArgumentException(
-                            "the a is not in the field");
-                }
-        } else {
+            if (this.b.signum() < 0 || this.b.compareTo(p) >= 0) {
+                throw new IllegalArgumentException("the a is not in the field");
+            }
+        } else if (this.field instanceof ECFieldF2m) {
             int fieldSizeInBits = this.field.getFieldSize();
             if (!(this.a.bitLength() <= fieldSizeInBits)) {
                 throw new IllegalArgumentException("the a is not in the field");
@@ -141,11 +143,10 @@
         if (!(other instanceof EllipticCurve)) {
             return false;
         }
-        EllipticCurve otherEc = (EllipticCurve)other;
-        return this.field.equals(otherEc.field) &&
-               this.a.equals(otherEc.a) &&
-               this.b.equals(otherEc.b) &&
-               Arrays.equals(this.seed, otherEc.seed);
+        EllipticCurve otherEc = (EllipticCurve) other;
+        return this.field.equals(otherEc.field) && this.a.equals(otherEc.a)
+                && this.b.equals(otherEc.b)
+                && Arrays.equals(this.seed, otherEc.seed);
     }
 
     /**
@@ -159,8 +160,8 @@
             hash0 = hash0 * 31 + a.hashCode();
             hash0 = hash0 * 31 + b.hashCode();
             if (seed != null) {
-                for (int i=0; i<seed.length; i++) {
-                    hash0 = hash0*31 + seed[i];
+                for (int i = 0; i < seed.length; i++) {
+                    hash0 = hash0 * 31 + seed[i];
                 }
             } else {
                 hash0 = hash0 * 31;

Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/spec/EllipticCurveTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/spec/EllipticCurveTest.java?rev=424250&r1=424249&r2=424250&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/spec/EllipticCurveTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/spec/EllipticCurveTest.java Fri Jul 21 03:04:47 2006
@@ -83,9 +83,7 @@
         try {
             new EllipticCurve(f, a, b, seed);
             fail("#1: Expected NPE not thrown");
-        } catch (NullPointerException ok) {
-        }
-
+        } catch (NullPointerException ok) {}
 
         // test case 2 parameters set,
         f = new ECFieldFp(BigInteger.valueOf(23L));
@@ -96,9 +94,7 @@
         try {
             new EllipticCurve(f, a, b, seed);
             fail("#2: Expected NPE not thrown");
-        } catch (NullPointerException ok) {
-        }
-
+        } catch (NullPointerException ok) {}
 
         // test case 3 parameters set,
         f = new ECFieldFp(BigInteger.valueOf(23L));
@@ -109,8 +105,7 @@
         try {
             new EllipticCurve(f, a, b, seed);
             fail("#3: Expected NPE not thrown");
-        } catch (NullPointerException ok) {
-        }
+        } catch (NullPointerException ok) {}
     }
 
     /**
@@ -135,9 +130,7 @@
         try {
             new EllipticCurve(f, a, b, seed);
             fail("#1: Expected IAE not thrown");
-        } catch (IllegalArgumentException ok) {
-        }
-
+        } catch (IllegalArgumentException ok) {}
 
         // test case 1.1 parameters set,
         // b is not in field
@@ -149,8 +142,7 @@
         try {
             new EllipticCurve(f, a, b, seed);
             fail("#1.1: Expected IAE not thrown");
-        } catch (IllegalArgumentException ok) {
-        }
+        } catch (IllegalArgumentException ok) {}
 
         // test case 2 parameters set,
         // b is not in field
@@ -162,9 +154,7 @@
         try {
             new EllipticCurve(f, a, b, seed);
             fail("#2: Expected IAE not thrown");
-        } catch (IllegalArgumentException ok) {
-        }
-
+        } catch (IllegalArgumentException ok) {}
 
         // test case 3 parameters set,
         // both a and b are not in field
@@ -176,8 +166,7 @@
         try {
             new EllipticCurve(f, a, b, seed);
             fail("#3: Expected IAE not thrown");
-        } catch (IllegalArgumentException ok) {
-        }
+        } catch (IllegalArgumentException ok) {}
     }
 
     /**
@@ -198,14 +187,11 @@
         BigInteger b = BigInteger.valueOf(19L);
         byte[] seed = new byte[24];
 
-
         // perform test case 1
         try {
             new EllipticCurve(f, a, b, seed);
             fail("#1: Expected IAE not thrown");
-        } catch (IllegalArgumentException ok) {
-        }
-
+        } catch (IllegalArgumentException ok) {}
 
         // test case 2 parameters set,
         // b is not in field
@@ -217,9 +203,7 @@
         try {
             new EllipticCurve(f, a, b, seed);
             fail("#2: Expected IAE not thrown");
-        } catch (IllegalArgumentException ok) {
-        }
-
+        } catch (IllegalArgumentException ok) {}
 
         // test case 3 parameters set,
         // both a and b are not in field
@@ -231,8 +215,7 @@
         try {
             new EllipticCurve(f, a, b, seed);
             fail("#3: Expected IAE not thrown");
-        } catch (IllegalArgumentException ok) {
-        }
+        } catch (IllegalArgumentException ok) {}
     }
 
     /**
@@ -250,7 +233,7 @@
         byte[] seedCopy = seed.clone();
         EllipticCurve c = new EllipticCurve(f, a, b, seedCopy);
         // modify array passed
-        seedCopy[0] = (byte)1;
+        seedCopy[0] = (byte) 1;
         // check that above modification did not changed
         // internal state of test object
         assertTrue(Arrays.equals(seed, c.getSeed()));
@@ -305,9 +288,7 @@
         try {
             new EllipticCurve(f, a, b);
             fail("#1: Expected NPE not thrown");
-        } catch (NullPointerException ok) {
-        }
-
+        } catch (NullPointerException ok) {}
 
         // test case 2 parameters set,
         f = new ECFieldFp(BigInteger.valueOf(23L));
@@ -317,9 +298,7 @@
         try {
             new EllipticCurve(f, a, b);
             fail("#2: Expected NPE not thrown");
-        } catch (NullPointerException ok) {
-        }
-
+        } catch (NullPointerException ok) {}
 
         // test case 3 parameters set,
         f = new ECFieldFp(BigInteger.valueOf(23L));
@@ -329,8 +308,7 @@
         try {
             new EllipticCurve(f, a, b);
             fail("#3: Expected NPE not thrown");
-        } catch (NullPointerException ok) {
-        }
+        } catch (NullPointerException ok) {}
     }
 
     /**
@@ -354,9 +332,7 @@
         try {
             new EllipticCurve(f, a, b);
             fail("#1: Expected IAE not thrown");
-        } catch (IllegalArgumentException ok) {
-        }
-
+        } catch (IllegalArgumentException ok) {}
 
         // test case 1.1 parameters set,
         // a is not in field
@@ -367,9 +343,7 @@
         try {
             new EllipticCurve(f, a, b);
             fail("#1.1: Expected IAE not thrown");
-        } catch (IllegalArgumentException ok) {
-        }
-
+        } catch (IllegalArgumentException ok) {}
 
         // test case 2 parameters set,
         // b is not in field
@@ -380,9 +354,7 @@
         try {
             new EllipticCurve(f, a, b);
             fail("#2: Expected IAE not thrown");
-        } catch (IllegalArgumentException ok) {
-        }
-
+        } catch (IllegalArgumentException ok) {}
 
         // test case 3 parameters set,
         // both a and b are not in field
@@ -393,8 +365,7 @@
         try {
             new EllipticCurve(f, a, b);
             fail("#3: Expected IAE not thrown");
-        } catch (IllegalArgumentException ok) {
-        }
+        } catch (IllegalArgumentException ok) {}
     }
 
     /**
@@ -417,9 +388,7 @@
         try {
             new EllipticCurve(f, a, b);
             fail("#1: Expected IAE not thrown");
-        } catch (IllegalArgumentException ok) {
-        }
-
+        } catch (IllegalArgumentException ok) {}
 
         // test case 2 parameters set,
         // b is not in field
@@ -430,9 +399,7 @@
         try {
             new EllipticCurve(f, a, b);
             fail("#2: Expected IAE not thrown");
-        } catch (IllegalArgumentException ok) {
-        }
-
+        } catch (IllegalArgumentException ok) {}
 
         // test case 3 parameters set,
         // both a and b are not in field
@@ -443,8 +410,7 @@
         try {
             new EllipticCurve(f, a, b);
             fail("#3: Expected IAE not thrown");
-        } catch (IllegalArgumentException ok) {
-        }
+        } catch (IllegalArgumentException ok) {}
     }
 
     /**
@@ -466,6 +432,18 @@
     }
 
     /**
+     * @tests java/security/spec/EllipticCurve#EllipticCurve(EcField,BigInteger,BigInteger)
+     */
+    public final void testEllipticCurveECFieldBigIntegerBigInteger05() {
+        // Regression for Harmony-731
+        EllipticCurve ec = new EllipticCurve(new testECField(), BigInteger
+                .valueOf(4L), BigInteger.ONE);
+        assertEquals("incorrect a", ec.getA(), BigInteger.valueOf(4L));
+        assertEquals("incorrect b", ec.getB(), BigInteger.ONE);
+        assertEquals("incorrect size", ec.getField().getFieldSize(), 2);
+    }
+
+    /**
      * Test for <code>getB()</code> method<br>
      * Assertion: returns coefficient <code>b</code><br>
      * Test preconditions: <code>ECFieldF2m</code> instance
@@ -536,7 +514,7 @@
         EllipticCurve c = new EllipticCurve(f, a, b, seed.clone());
         byte[] seedRet = c.getSeed();
         // modify returned array
-        seedRet[0] = (byte)1;
+        seedRet[0] = (byte) 1;
         // check that above modification did not changed
         // internal state of test object
         assertTrue(Arrays.equals(seed, c.getSeed()));
@@ -562,7 +540,7 @@
     /**
      * @tests java.security.spec.EllipticCurve#getSeed()
      * Assertion: null if not specified
-     */    
+     */
     public final void testGetSeed04() {
         //Regression for HARMONY-732
         ECFieldFp f = new ECFieldFp(BigInteger.valueOf(23L));
@@ -578,52 +556,38 @@
      */
     public final void testEqualsObject01() {
         // test case 1: must be equal to itself
-        EllipticCurve c2=null, c1 =
-            new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
-                    BigInteger.ONE,
-                    BigInteger.valueOf(19L));
+        EllipticCurve c2 = null, c1 = new EllipticCurve(new ECFieldFp(
+                BigInteger.valueOf(23L)), BigInteger.ONE, BigInteger
+                .valueOf(19L));
         assertTrue(c1.equals(c1));
 
         // test case 2: equal objects
         c1 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
-                BigInteger.ONE,
-                BigInteger.valueOf(19L));
+                BigInteger.ONE, BigInteger.valueOf(19L));
         c2 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
-                BigInteger.valueOf(1L),
-                BigInteger.valueOf(19L));
+                BigInteger.valueOf(1L), BigInteger.valueOf(19L));
         assertTrue(c1.equals(c2) && c2.equals(c1));
 
         // test case 3: equal objects with seed not null
         c1 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
-                BigInteger.ONE,
-                BigInteger.valueOf(19L),
-                new byte[24]);
+                BigInteger.ONE, BigInteger.valueOf(19L), new byte[24]);
         c2 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
-                BigInteger.valueOf(1L),
-                BigInteger.valueOf(19L),
-                new byte[24]);
+                BigInteger.valueOf(1L), BigInteger.valueOf(19L), new byte[24]);
         assertTrue(c1.equals(c2) && c2.equals(c1));
 
         // test case 4: equal object and subclass object
         c1 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
-                BigInteger.ONE,
-                BigInteger.valueOf(19L),
-                new byte[24]);
-        MyEllipticCurve c3 = new MyEllipticCurve(
-                new ECFieldFp(BigInteger.valueOf(23L)),
-                BigInteger.ONE,
-                BigInteger.valueOf(19L),
+                BigInteger.ONE, BigInteger.valueOf(19L), new byte[24]);
+        MyEllipticCurve c3 = new MyEllipticCurve(new ECFieldFp(BigInteger
+                .valueOf(23L)), BigInteger.ONE, BigInteger.valueOf(19L),
                 new byte[24]);
         assertTrue(c1.equals(c3) && c3.equals(c1));
 
         // test case 5: equal objects
         c1 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
-                BigInteger.ONE,
-                BigInteger.valueOf(19L));
+                BigInteger.ONE, BigInteger.valueOf(19L));
         c2 = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
-                BigInteger.valueOf(1L),
-                BigInteger.valueOf(19L),
-                null);
+                BigInteger.valueOf(1L), BigInteger.valueOf(19L), null);
         assertTrue(c1.equals(c2) && c2.equals(c1));
     }
 
@@ -635,19 +599,14 @@
      */
     public final void testHashCode01() {
         int hc = 0;
-        EllipticCurve f = new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
-                BigInteger.ONE,
-                BigInteger.valueOf(19L),
+        EllipticCurve f = new EllipticCurve(new ECFieldFp(BigInteger
+                .valueOf(23L)), BigInteger.ONE, BigInteger.valueOf(19L),
                 new byte[24]);
         hc = f.hashCode();
-        assertTrue(hc == f.hashCode() &&
-                hc == f.hashCode() &&
-                hc == f.hashCode() &&
-                hc == f.hashCode() &&
-                hc == f.hashCode() &&
-                hc == f.hashCode() &&
-                hc == f.hashCode() &&
-                hc == f.hashCode());
+        assertTrue(hc == f.hashCode() && hc == f.hashCode()
+                && hc == f.hashCode() && hc == f.hashCode()
+                && hc == f.hashCode() && hc == f.hashCode()
+                && hc == f.hashCode() && hc == f.hashCode());
     }
 
     /**
@@ -657,28 +616,32 @@
      * on equal (according to the <code>equals(Object)</code> method) objects. 
      */
     public final void testHashCode02() {
-        assertEquals(
-                new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
-                        BigInteger.ONE,
-                        BigInteger.valueOf(19L),
-                        new byte[24]).hashCode(),
-                new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
-                        BigInteger.ONE,
-                        BigInteger.valueOf(19L),
-                        new byte[24]).hashCode());
+        assertEquals(new EllipticCurve(new ECFieldFp(BigInteger.valueOf(23L)),
+                BigInteger.ONE, BigInteger.valueOf(19L), new byte[24])
+                .hashCode(), new EllipticCurve(new ECFieldFp(BigInteger
+                .valueOf(23L)), BigInteger.ONE, BigInteger.valueOf(19L),
+                new byte[24]).hashCode());
     }
 
     //
     // Private stuff
     //
 
+    class testECField implements ECField {
+
+        public int getFieldSize() {
+            return 2;
+        }
+    }
+
     /**
      * EllipticCurve subclass for testing purposes
      * 
      */
     private static class MyEllipticCurve extends EllipticCurve {
+
         MyEllipticCurve(ECField f, BigInteger a, BigInteger b, byte[] seed) {
-            super(f,a,b,seed);
+            super(f, a, b, seed);
         }
     }
 }