< prev index next >

test/jdk/sun/security/ec/xec/TestXDH.java

Print this page




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8171277
  27  * @summary Test XDH key agreement
  28  * @library /test/lib
  29  * @build jdk.test.lib.Convert
  30  * @run main TestXDH
  31  */
  32 
  33 import java.security.*;
  34 import java.security.spec.*;
  35 import javax.crypto.*;
  36 import java.util.Arrays;
  37 import java.math.BigInteger;
  38 import jdk.test.lib.Convert;
  39 
  40 public class TestXDH {
  41 
  42     public static void main(String[] args) throws Exception {
  43 
  44         runBasicTests();
  45         runKAT();
  46         runSmallOrderTest();


  49     }
  50 
  51     private static void runBasicTests() throws Exception {
  52         runBasicTest("XDH", null);
  53         runBasicTest("XDH", 255);
  54         runBasicTest("XDH", 448);
  55         runBasicTest("XDH", "X25519");
  56         runBasicTest("XDH", "X448");
  57         runBasicTest("X25519", null);
  58         runBasicTest("X448", null);
  59         runBasicTest("1.3.101.110", null);
  60         runBasicTest("1.3.101.111", null);
  61         runBasicTest("OID.1.3.101.110", null);
  62         runBasicTest("OID.1.3.101.111", null);
  63     }
  64 
  65     private static void runBasicTest(String name, Object param)
  66         throws Exception {
  67 
  68         KeyPairGenerator kpg = KeyPairGenerator.getInstance(name);

  69         if (param instanceof Integer) {
  70             kpg.initialize((Integer) param);
  71         } else if (param instanceof String) {
  72             kpg.initialize(new NamedParameterSpec((String) param));

  73         }
  74         KeyPair kp = kpg.generateKeyPair();
  75 
  76         KeyAgreement ka = KeyAgreement.getInstance(name);
  77         ka.init(kp.getPrivate());
  78         ka.doPhase(kp.getPublic(), true);
  79 
  80         byte[] secret = ka.generateSecret();
  81 
  82         KeyFactory kf = KeyFactory.getInstance(name);
  83         // Test with X509 and PKCS8 key specs
  84         X509EncodedKeySpec pubSpec =
  85             kf.getKeySpec(kp.getPublic(), X509EncodedKeySpec.class);
  86         PKCS8EncodedKeySpec priSpec =
  87             kf.getKeySpec(kp.getPrivate(), PKCS8EncodedKeySpec.class);
  88 
  89         PublicKey pubKey = kf.generatePublic(pubSpec);
  90         PrivateKey priKey = kf.generatePrivate(priSpec);
  91 
  92         ka.init(priKey);
  93         ka.doPhase(pubKey, true);
  94         byte[] secret2 = ka.generateSecret();
  95         if (!Arrays.equals(secret, secret2)) {
  96             throw new RuntimeException("Arrays not equal");
  97         }










  98 
  99         // test with XDH key specs
 100         XECPublicKeySpec xdhPublic =
 101             kf.getKeySpec(kp.getPublic(), XECPublicKeySpec.class);
 102         XECPrivateKeySpec xdhPrivate =
 103             kf.getKeySpec(kp.getPrivate(), XECPrivateKeySpec.class);
 104         PublicKey pubKey2 = kf.generatePublic(xdhPublic);
 105         PrivateKey priKey2 = kf.generatePrivate(xdhPrivate);
 106         ka.init(priKey2);
 107         ka.doPhase(pubKey2, true);
 108         byte[] secret3 = ka.generateSecret();
 109         if (!Arrays.equals(secret, secret3)) {
 110             throw new RuntimeException("Arrays not equal");
 111         }
 112     }
 113 
 114     private static void runSmallOrderTest() throws Exception {
 115         // Ensure that small-order points are rejected
 116 
 117         // X25519




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8171277 8206915
  27  * @summary Test XDH key agreement
  28  * @library /test/lib
  29  * @build jdk.test.lib.Convert
  30  * @run main TestXDH
  31  */
  32 
  33 import java.security.*;
  34 import java.security.spec.*;
  35 import javax.crypto.*;
  36 import java.util.Arrays;
  37 import java.math.BigInteger;
  38 import jdk.test.lib.Convert;
  39 
  40 public class TestXDH {
  41 
  42     public static void main(String[] args) throws Exception {
  43 
  44         runBasicTests();
  45         runKAT();
  46         runSmallOrderTest();


  49     }
  50 
  51     private static void runBasicTests() throws Exception {
  52         runBasicTest("XDH", null);
  53         runBasicTest("XDH", 255);
  54         runBasicTest("XDH", 448);
  55         runBasicTest("XDH", "X25519");
  56         runBasicTest("XDH", "X448");
  57         runBasicTest("X25519", null);
  58         runBasicTest("X448", null);
  59         runBasicTest("1.3.101.110", null);
  60         runBasicTest("1.3.101.111", null);
  61         runBasicTest("OID.1.3.101.110", null);
  62         runBasicTest("OID.1.3.101.111", null);
  63     }
  64 
  65     private static void runBasicTest(String name, Object param)
  66         throws Exception {
  67 
  68         KeyPairGenerator kpg = KeyPairGenerator.getInstance(name);
  69         AlgorithmParameterSpec paramSpec = null;
  70         if (param instanceof Integer) {
  71             kpg.initialize((Integer) param);
  72         } else if (param instanceof String) {
  73             paramSpec = new NamedParameterSpec((String) param);
  74             kpg.initialize(paramSpec);
  75         }
  76         KeyPair kp = kpg.generateKeyPair();
  77 
  78         KeyAgreement ka = KeyAgreement.getInstance(name);
  79         ka.init(kp.getPrivate(), paramSpec);
  80         ka.doPhase(kp.getPublic(), true);
  81 
  82         byte[] secret = ka.generateSecret();
  83 
  84         KeyFactory kf = KeyFactory.getInstance(name);
  85         // Test with X509 and PKCS8 key specs
  86         X509EncodedKeySpec pubSpec =
  87             kf.getKeySpec(kp.getPublic(), X509EncodedKeySpec.class);
  88         PKCS8EncodedKeySpec priSpec =
  89             kf.getKeySpec(kp.getPrivate(), PKCS8EncodedKeySpec.class);
  90 
  91         PublicKey pubKey = kf.generatePublic(pubSpec);
  92         PrivateKey priKey = kf.generatePrivate(priSpec);
  93 
  94         ka.init(priKey);
  95         ka.doPhase(pubKey, true);
  96         byte[] secret2 = ka.generateSecret();
  97         if (!Arrays.equals(secret, secret2)) {
  98             throw new RuntimeException("Arrays not equal");
  99         }
 100 
 101         // make sure generateSecret() resets the state to after init()
 102         try {
 103             ka.generateSecret();
 104             throw new RuntimeException("generateSecret does not reset state");
 105         } catch (IllegalStateException ex) {
 106             // do nothing---this is expected
 107         }
 108         ka.doPhase(pubKey, true);
 109         ka.generateSecret();
 110 
 111         // test with XDH key specs
 112         XECPublicKeySpec xdhPublic =
 113             kf.getKeySpec(kp.getPublic(), XECPublicKeySpec.class);
 114         XECPrivateKeySpec xdhPrivate =
 115             kf.getKeySpec(kp.getPrivate(), XECPrivateKeySpec.class);
 116         PublicKey pubKey2 = kf.generatePublic(xdhPublic);
 117         PrivateKey priKey2 = kf.generatePrivate(xdhPrivate);
 118         ka.init(priKey2);
 119         ka.doPhase(pubKey2, true);
 120         byte[] secret3 = ka.generateSecret();
 121         if (!Arrays.equals(secret, secret3)) {
 122             throw new RuntimeException("Arrays not equal");
 123         }
 124     }
 125 
 126     private static void runSmallOrderTest() throws Exception {
 127         // Ensure that small-order points are rejected
 128 
 129         // X25519


< prev index next >