jdk/test/sun/security/pkcs11/ec/TestCurves.java
changeset 17491 7a33824ec8c5
parent 12685 8a448b5b9006
child 19067 5271291b7121
equal deleted inserted replaced
17467:374c1cceefff 17491:7a33824ec8c5
    36 import java.security.*;
    36 import java.security.*;
    37 import java.security.spec.*;
    37 import java.security.spec.*;
    38 
    38 
    39 import javax.crypto.*;
    39 import javax.crypto.*;
    40 
    40 
    41 // XXX no public API to enumerate supported named curves
       
    42 import sun.security.ec.NamedCurve;
       
    43 
       
    44 public class TestCurves extends PKCS11Test {
    41 public class TestCurves extends PKCS11Test {
    45 
    42 
    46     public static void main(String[] args) throws Exception {
    43     public static void main(String[] args) throws Exception {
    47         main(new TestCurves());
    44         main(new TestCurves());
    48     }
    45     }
    55 
    52 
    56         Random random = new Random();
    53         Random random = new Random();
    57         byte[] data = new byte[2048];
    54         byte[] data = new byte[2048];
    58         random.nextBytes(data);
    55         random.nextBytes(data);
    59 
    56 
    60         Collection<? extends ECParameterSpec> curves =
    57         Vector<ECParameterSpec> curves = getKnownCurves(p);
    61             NamedCurve.knownECParameterSpecs();
    58 
    62         for (ECParameterSpec params : curves) {
    59         for (ECParameterSpec params : curves) {
    63             System.out.println("Testing " + params + "...");
    60             System.out.println("Testing " + params + "...");
    64             KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);
    61             KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", p);
    65             kpg.initialize(params);
    62             kpg.initialize(params);
    66             KeyPair kp1, kp2;
    63             KeyPair kp1, kp2;
    90         }
    87         }
    91 
    88 
    92         System.out.println("OK");
    89         System.out.println("OK");
    93     }
    90     }
    94 
    91 
       
    92     private static Vector<ECParameterSpec>
       
    93             getKnownCurves(Provider p) throws Exception {
       
    94 
       
    95         int index;
       
    96         int begin;
       
    97         int end;
       
    98         String curve;
       
    99         Vector<ECParameterSpec> results = new Vector<ECParameterSpec>();
       
   100         String kcProp =
       
   101             p.getProperty("AlgorithmParameters.EC SupportedCurves");
       
   102 
       
   103         if (kcProp == null) {
       
   104             throw new RuntimeException(
       
   105             "\"AlgorithmParameters.EC SupportedCurves property\" not found");
       
   106         }
       
   107 
       
   108         index = 0;
       
   109         for (;;) {
       
   110             // Each set of curve names is enclosed with brackets.
       
   111             begin = kcProp.indexOf('[', index);
       
   112             end = kcProp.indexOf(']', index);
       
   113             if (begin == -1 || end == -1) {
       
   114                 break;
       
   115             }
       
   116 
       
   117             /*
       
   118              * Each name is separated by a comma.
       
   119              * Just get the first name in the set.
       
   120              */
       
   121             index = end + 1;
       
   122             begin++;
       
   123             end = kcProp.indexOf(',', begin);
       
   124             if (end == -1) {
       
   125                 // Only one name in the set.
       
   126                 end = index -1;
       
   127             }
       
   128 
       
   129             curve = kcProp.substring(begin, end);
       
   130 
       
   131             results.add(getECParameterSpec(p, curve));
       
   132         }
       
   133 
       
   134         if (results.size() == 0) {
       
   135             throw new RuntimeException("No supported EC curves found");
       
   136         }
       
   137 
       
   138         return results;
       
   139     }
       
   140 
       
   141     private static ECParameterSpec getECParameterSpec(Provider p, String name)
       
   142             throws Exception {
       
   143 
       
   144         AlgorithmParameters parameters =
       
   145             AlgorithmParameters.getInstance("EC", p);
       
   146 
       
   147         parameters.init(new ECGenParameterSpec(name));
       
   148 
       
   149         return parameters.getParameterSpec(ECParameterSpec.class);
       
   150     }
       
   151 
    95     private static void testSigning(Provider p, String algorithm,
   152     private static void testSigning(Provider p, String algorithm,
    96             byte[] data, KeyPair kp1, KeyPair kp2) throws Exception {
   153             byte[] data, KeyPair kp1, KeyPair kp2) throws Exception {
    97         // System.out.print("  " + algorithm);
   154         // System.out.print("  " + algorithm);
    98         Signature s = Signature.getInstance(algorithm, p);
   155         Signature s = Signature.getInstance(algorithm, p);
    99         s.initSign(kp1.getPrivate());
   156         s.initSign(kp1.getPrivate());
   113         r = s.verify(sig);
   170         r = s.verify(sig);
   114         if (r) {
   171         if (r) {
   115             throw new Exception("Signature should not verify");
   172             throw new Exception("Signature should not verify");
   116         }
   173         }
   117     }
   174     }
   118 
       
   119 
       
   120 }
   175 }