1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
/*
* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 6330287 6331386 7044060
* @summary verify that DHKeyPairGenerator returns keys of the expected size
* (modulus and exponent)
* -and-
* DHKeyPairGenerator is using BigInteger.setBit
* @author Andreas Sterbenz
*/
import java.math.BigInteger;
import java.util.Arrays;
import java.security.*;
import javax.crypto.*;
import javax.crypto.interfaces.*;
import javax.crypto.spec.*;
/*
* NOTE: BigInteger's bitlength() doesn't report leading zeros, only
* the number of bits needed to actually represent the number. i.e.
*
* new BigInteger("4").bitLength() = 3
*
* Since the private key x can vary 1 <= x <= p-2, we can't do any
* bitlength-based calculations here. But we can check that p conforms
* as expected.
*
* If not specified, we're currently using an lsize of Math.max(384, psize/2).
*/
public class TestExponentSize {
/*
* Sizes and values for various lengths.
*/
private enum Sizes {
two56(256), three84(384), five12(512), seven68(768), ten24(1024),
twenty48(2048);
private final int intSize;
private final BigInteger bigIntValue;
Sizes(int size) {
intSize = size;
byte [] bits = new byte[intSize/8];
Arrays.fill(bits, (byte)0xff);
bigIntValue = new BigInteger(1, bits);
}
int getIntSize() {
return intSize;
}
BigInteger getBigIntValue() {
return bigIntValue;
}
}
public static void main(String[] args) throws Exception {
KeyPair kp;
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", "SunJCE");
// Sun's default uses a default psize of 2048 and
// lsize of (pSize / 2) but at least 384 bits
kp = kpg.generateKeyPair();
checkKeyPair(kp, Sizes.twenty48, Sizes.ten24);
DHPublicKey publicKey = (DHPublicKey)kp.getPublic();
BigInteger p = publicKey.getParams().getP();
BigInteger g = publicKey.getParams().getG();
kpg.initialize(Sizes.ten24.getIntSize());
kp = kpg.generateKeyPair();
checkKeyPair(kp, Sizes.ten24, Sizes.five12);
kpg.initialize(new DHParameterSpec(p, g, Sizes.ten24.getIntSize()));
kp = kpg.generateKeyPair();
checkKeyPair(kp, Sizes.twenty48, Sizes.ten24);
kpg.initialize(new DHParameterSpec(p, g, Sizes.five12.getIntSize()));
kp = kpg.generateKeyPair();
checkKeyPair(kp, Sizes.twenty48, Sizes.five12);
kpg.initialize(new DHParameterSpec(p, g, Sizes.two56.getIntSize()));
kp = kpg.generateKeyPair();
checkKeyPair(kp, Sizes.twenty48, Sizes.two56);
kpg.initialize(Sizes.five12.getIntSize());
kp = kpg.generateKeyPair();
checkKeyPair(kp, Sizes.five12, Sizes.three84);
kpg.initialize(Sizes.seven68.getIntSize());
kp = kpg.generateKeyPair();
checkKeyPair(kp, Sizes.seven68, Sizes.three84);
// test w/ only pSize
kpg.initialize(Sizes.twenty48.getIntSize());
kp = kpg.generateKeyPair();
checkKeyPair(kp, Sizes.twenty48, Sizes.ten24);
publicKey = (DHPublicKey)kp.getPublic();
p = publicKey.getParams().getP();
g = publicKey.getParams().getG();
// test w/ all values specified
kpg.initialize(new DHParameterSpec(p, g, Sizes.five12.getIntSize()));
kp = kpg.generateKeyPair();
checkKeyPair(kp, Sizes.twenty48, Sizes.five12);
System.out.println("OK");
}
private static void checkKeyPair(KeyPair kp, Sizes modulusSize,
Sizes exponentSize) throws Exception {
System.out.println("Checking (" + modulusSize + ", " +
exponentSize + ")");
DHPrivateKey privateKey = (DHPrivateKey)kp.getPrivate();
BigInteger p = privateKey.getParams().getP();
BigInteger x = privateKey.getX();
if (p.bitLength() != modulusSize.getIntSize()) {
throw new Exception("Invalid modulus size: " + p.bitLength());
}
if (x.bitLength() > exponentSize.getIntSize()) {
throw new Exception("X has more bits than expected: " +
x.bitLength());
}
BigInteger pMinus2 =
p.subtract(BigInteger.ONE).subtract(BigInteger.ONE);
if ((x.compareTo(BigInteger.ONE) < 0 ||
(x.compareTo(pMinus2)) > 0)) {
throw new Exception(
"X outside range 1<=x<p-2: x: " + x + " p: " + p);
}
}
}
|