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