jdk/test/com/oracle/security/ucrypto/TestRSA.java
changeset 33407 e0e9a702fdfb
parent 27182 4525d13b8af1
equal deleted inserted replaced
33406:4680f7495292 33407:e0e9a702fdfb
     1 /*
     1 /*
     2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
   169         KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
   169         KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
   170         kpg.initialize(keyLength);
   170         kpg.initialize(keyLength);
   171         return kpg.generateKeyPair();
   171         return kpg.generateKeyPair();
   172     }
   172     }
   173 
   173 
   174     private static KeyPair genPredefinedRSAKeyPair() throws Exception {
   174     private static KeyPair genPredefinedRSAKeyPair(String prov) throws Exception {
   175         KeyFactory kf = KeyFactory.getInstance("RSA");
   175         KeyFactory kf;
   176         BigInteger mod = new BigInteger(MOD);
   176         if (prov == null) {
   177         BigInteger pub = new BigInteger(PUB_EXP);
   177             kf = KeyFactory.getInstance("RSA");
       
   178             System.out.println("Using default KeyFactory:  "+kf.getProvider().getName());
       
   179         } else {
       
   180             kf = KeyFactory.getInstance("RSA", prov);
       
   181             System.out.println("Using specified KeyFactory:  "+kf.getProvider().getName());
       
   182         }
       
   183         BigInteger mod = new BigInteger(1, MOD);
       
   184         BigInteger pub = new BigInteger(1, PUB_EXP);
   178 
   185 
   179         PrivateKey privKey = kf.generatePrivate
   186         PrivateKey privKey = kf.generatePrivate
   180             (new RSAPrivateCrtKeySpec
   187             (new RSAPrivateCrtKeySpec
   181              (mod, pub, new BigInteger(PRIV_EXP),
   188              (mod, pub, new BigInteger(1, PRIV_EXP),
   182               new BigInteger(PRIME_P), new BigInteger(PRIME_Q),
   189               new BigInteger(1, PRIME_P), new BigInteger(1, PRIME_Q),
   183               new BigInteger(EXP_P), new BigInteger(EXP_Q),
   190               new BigInteger(1, EXP_P), new BigInteger(1, EXP_Q),
   184               new BigInteger(CRT_COEFF)));
   191               new BigInteger(1, CRT_COEFF)));
   185         PublicKey pubKey = kf.generatePublic(new RSAPublicKeySpec(mod, pub));
   192         PublicKey pubKey = kf.generatePublic(new RSAPublicKeySpec(mod, pub));
   186         return new KeyPair(pubKey, privKey);
   193         return new KeyPair(pubKey, privKey);
   187     }
   194     }
   188 
   195 
   189     private static final String CIP_ALGOS[] = {
   196     private static final String CIP_ALGOS[] = {
   208         main(new TestRSA(), null);
   215         main(new TestRSA(), null);
   209     }
   216     }
   210 
   217 
   211     public void doTest(Provider prov) throws Exception {
   218     public void doTest(Provider prov) throws Exception {
   212         // first test w/ predefine KeyPair
   219         // first test w/ predefine KeyPair
   213         KeyPair pkp = genPredefinedRSAKeyPair();
       
   214         System.out.println("Test against Predefined RSA Key Pair");
   220         System.out.println("Test against Predefined RSA Key Pair");
       
   221         KeyPair pkp = genPredefinedRSAKeyPair("SunPKCS11-Solaris");
   215         testCipher(pkp, 128, true, prov);
   222         testCipher(pkp, 128, true, prov);
   216         testSignature(pkp, true, prov);
   223         testSignature(pkp, true, prov);
   217 
   224 
       
   225         pkp = genPredefinedRSAKeyPair("SunRsaSign");
       
   226         testCipher(pkp, 128, true, prov);
       
   227         testSignature(pkp, true, prov);
       
   228 
       
   229         pkp = genPredefinedRSAKeyPair(null);
       
   230         testCipher(pkp, 128, true, prov);
       
   231         testSignature(pkp, true, prov);
       
   232 
       
   233         System.out.println("Running key length test loop");
   218         for (int i = 0; i < 10; i++) {
   234         for (int i = 0; i < 10; i++) {
   219             // then test w/ various key lengths
   235             // then test w/ various key lengths
   220             int keyLens[] = { 1024, 2048 };
   236             int keyLens[] = { 1024, 2048 };
   221             kp = new KeyPair[keyLens.length];
   237             kp = new KeyPair[keyLens.length];
   222 
   238