TestDH2048.java revision 14174:0bb2dfd0852c
197403Sobrien/*
297403Sobrien * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
397403Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
497403Sobrien *
597403Sobrien * This code is free software; you can redistribute it and/or modify it
697403Sobrien * under the terms of the GNU General Public License version 2 only, as
797403Sobrien * published by the Free Software Foundation.
897403Sobrien *
997403Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1097403Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1197403Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1297403Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1397403Sobrien * accompanied this code).
1497403Sobrien *
1597403Sobrien * You should have received a copy of the GNU General Public License version
1697403Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
1797403Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1897403Sobrien *
1997403Sobrien * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2097403Sobrien * or visit www.oracle.com if you need additional information or have any
2197403Sobrien * questions.
2297403Sobrien */
2397403Sobrien
2497403Sobrien/**
2597403Sobrien * @test
2697403Sobrien * @bug 7196382 8072452
2797403Sobrien * @summary Ensure that DH key pairs can be generated for 512 - 8192 bits
2897403Sobrien * @author Valerie Peng
2997403Sobrien * @library ..
3097403Sobrien * @run main/othervm TestDH2048
3197403Sobrien * @run main/othervm TestDH2048 sm
3297403Sobrien */
3397403Sobrien
3497403Sobrienimport java.security.InvalidParameterException;
3597403Sobrienimport java.security.KeyPair;
3697403Sobrienimport java.security.KeyPairGenerator;
3797403Sobrienimport java.security.Provider;
3897403Sobrien
3997403Sobrienpublic class TestDH2048 extends PKCS11Test {
4097403Sobrien
4197403Sobrien    private static void checkUnsupportedKeySize(KeyPairGenerator kpg, int ks)
4297403Sobrien        throws Exception {
4397403Sobrien        try {
4497403Sobrien            kpg.initialize(ks);
4597403Sobrien            throw new Exception("Expected IPE not thrown for " + ks);
4697403Sobrien        } catch (InvalidParameterException ipe) {
4797403Sobrien        }
4897403Sobrien    }
4997403Sobrien
5097403Sobrien    @Override
5197403Sobrien    public void main(Provider p) throws Exception {
5297403Sobrien        if (p.getService("KeyPairGenerator", "DH") == null) {
5397403Sobrien            System.out.println("KPG for DH not supported, skipping");
5497403Sobrien            return;
5597403Sobrien        }
5697403Sobrien        KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", p);
5797403Sobrien        kpg.initialize(512);
5897403Sobrien        KeyPair kp1 = kpg.generateKeyPair();
5997403Sobrien
6097403Sobrien        kpg.initialize(768);
6197403Sobrien        kp1 = kpg.generateKeyPair();
6297403Sobrien
63        kpg.initialize(1024);
64        kp1 = kpg.generateKeyPair();
65
66        kpg.initialize(1536);
67        kp1 = kpg.generateKeyPair();
68
69        kpg.initialize(2048);
70        kp1 = kpg.generateKeyPair();
71
72        try {
73            kpg.initialize(3072);
74            kp1 = kpg.generateKeyPair();
75
76            kpg.initialize(4096);
77            kp1 = kpg.generateKeyPair();
78
79            kpg.initialize(6144);
80            kp1 = kpg.generateKeyPair();
81
82            kpg.initialize(8192);
83            kp1 = kpg.generateKeyPair();
84        } catch (InvalidParameterException ipe) {
85            // NSS (as of version 3.13) has a hard coded maximum limit
86            // of 2236 or 3072 bits for DHE keys.
87            System.out.println("4096-bit DH key pair generation: " + ipe);
88            if (!p.getName().equals("SunPKCS11-NSS")) {
89                throw ipe;
90            }
91        }
92
93        // key size must be multiples of 64 though
94        checkUnsupportedKeySize(kpg, 2048 + 63);
95        checkUnsupportedKeySize(kpg, 3072 + 32);
96    }
97
98    public static void main(String[] args) throws Exception {
99        main(new TestDH2048(), args);
100    }
101}
102