J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
Sean Mullan | e868165 | 2016-03-07 10:10:04 -0500 | [diff] [blame] | 2 | * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 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 |
Kelly O'Hair | fe008ae | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. Oracle designates this |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 8 | * particular file as subject to the "Classpath" exception as provided |
Kelly O'Hair | fe008ae | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 9 | * by Oracle in the LICENSE file that accompanied this code. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
Kelly O'Hair | fe008ae | 2010-05-25 15:58:33 -0700 | [diff] [blame] | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | * or visit www.oracle.com if you need additional information or have any |
| 23 | * questions. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | package com.sun.crypto.provider; |
| 27 | |
Xue-Lei Andrew Fan | 7f30460 | 2016-04-15 11:09:18 +0000 | [diff] [blame] | 28 | import java.math.BigInteger; |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 29 | import java.security.*; |
| 30 | import java.security.spec.*; |
| 31 | import javax.crypto.spec.DHParameterSpec; |
| 32 | import javax.crypto.spec.DHGenParameterSpec; |
| 33 | |
| 34 | /* |
| 35 | * This class generates parameters for the Diffie-Hellman algorithm. |
| 36 | * The parameters are a prime, a base, and optionally the length in bits of |
| 37 | * the private value. |
| 38 | * |
| 39 | * <p>The Diffie-Hellman parameter generation accepts the size in bits of the |
| 40 | * prime modulus and the size in bits of the random exponent as input. |
Sean Mullan | e868165 | 2016-03-07 10:10:04 -0500 | [diff] [blame] | 41 | * The size of the prime modulus defaults to 2048 bits. |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 42 | * |
| 43 | * @author Jan Luehe |
| 44 | * |
| 45 | * |
| 46 | * @see java.security.AlgorithmParameters |
| 47 | * @see java.security.spec.AlgorithmParameterSpec |
| 48 | * @see DHParameters |
| 49 | */ |
Xue-Lei Andrew Fan | 7f30460 | 2016-04-15 11:09:18 +0000 | [diff] [blame] | 50 | public final class DHParameterGenerator extends AlgorithmParameterGeneratorSpi { |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 51 | |
| 52 | // The size in bits of the prime modulus |
Sean Mullan | e868165 | 2016-03-07 10:10:04 -0500 | [diff] [blame] | 53 | private int primeSize = 2048; |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 54 | |
| 55 | // The size in bits of the random exponent (private value) |
| 56 | private int exponentSize = 0; |
| 57 | |
| 58 | // The source of randomness |
| 59 | private SecureRandom random = null; |
| 60 | |
Valerie Peng | 2f4af22 | 2013-10-08 11:07:31 -0700 | [diff] [blame] | 61 | private static void checkKeySize(int keysize) |
Xue-Lei Andrew Fan | 7f30460 | 2016-04-15 11:09:18 +0000 | [diff] [blame] | 62 | throws InvalidParameterException { |
| 63 | |
| 64 | boolean supported = ((keysize == 2048) || (keysize == 3072) || |
| 65 | ((keysize >= 512) && (keysize <= 1024) && ((keysize & 0x3F) == 0))); |
| 66 | |
| 67 | if (!supported) { |
| 68 | throw new InvalidParameterException( |
| 69 | "DH key size must be multiple of 64 and range " + |
| 70 | "from 512 to 1024 (inclusive), or 2048, 3072. " + |
| 71 | "The specific key size " + keysize + " is not supported"); |
Valerie Peng | 2f4af22 | 2013-10-08 11:07:31 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 75 | /** |
| 76 | * Initializes this parameter generator for a certain keysize |
| 77 | * and source of randomness. |
| 78 | * The keysize is specified as the size in bits of the prime modulus. |
| 79 | * |
| 80 | * @param keysize the keysize (size of prime modulus) in bits |
| 81 | * @param random the source of randomness |
| 82 | */ |
Xue-Lei Andrew Fan | 7f30460 | 2016-04-15 11:09:18 +0000 | [diff] [blame] | 83 | @Override |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 84 | protected void engineInit(int keysize, SecureRandom random) { |
Xue-Lei Andrew Fan | 7f30460 | 2016-04-15 11:09:18 +0000 | [diff] [blame] | 85 | checkKeySize(keysize); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 86 | this.primeSize = keysize; |
| 87 | this.random = random; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Initializes this parameter generator with a set of parameter |
| 92 | * generation values, which specify the size of the prime modulus and |
| 93 | * the size of the random exponent, both in bits. |
| 94 | * |
Alexander Stepanov | 48e1903 | 2015-07-31 15:07:18 +0300 | [diff] [blame] | 95 | * @param genParamSpec the set of parameter generation values |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 96 | * @param random the source of randomness |
| 97 | * |
| 98 | * @exception InvalidAlgorithmParameterException if the given parameter |
| 99 | * generation values are inappropriate for this parameter generator |
| 100 | */ |
Xue-Lei Andrew Fan | 7f30460 | 2016-04-15 11:09:18 +0000 | [diff] [blame] | 101 | @Override |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 102 | protected void engineInit(AlgorithmParameterSpec genParamSpec, |
Xue-Lei Andrew Fan | 7f30460 | 2016-04-15 11:09:18 +0000 | [diff] [blame] | 103 | SecureRandom random) throws InvalidAlgorithmParameterException { |
| 104 | |
Valerie Peng | 2f4af22 | 2013-10-08 11:07:31 -0700 | [diff] [blame] | 105 | if (!(genParamSpec instanceof DHGenParameterSpec)) { |
| 106 | throw new InvalidAlgorithmParameterException |
| 107 | ("Inappropriate parameter type"); |
| 108 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 109 | |
Valerie Peng | 2f4af22 | 2013-10-08 11:07:31 -0700 | [diff] [blame] | 110 | DHGenParameterSpec dhParamSpec = (DHGenParameterSpec)genParamSpec; |
Valerie Peng | 2f4af22 | 2013-10-08 11:07:31 -0700 | [diff] [blame] | 111 | primeSize = dhParamSpec.getPrimeSize(); |
Valerie Peng | 2f4af22 | 2013-10-08 11:07:31 -0700 | [diff] [blame] | 112 | exponentSize = dhParamSpec.getExponentSize(); |
Xue-Lei Andrew Fan | 7f30460 | 2016-04-15 11:09:18 +0000 | [diff] [blame] | 113 | if ((exponentSize <= 0) || (exponentSize >= primeSize)) { |
| 114 | throw new InvalidAlgorithmParameterException( |
| 115 | "Exponent size (" + exponentSize + |
| 116 | ") must be positive and less than modulus size (" + |
| 117 | primeSize + ")"); |
| 118 | } |
| 119 | try { |
| 120 | checkKeySize(primeSize); |
| 121 | } catch (InvalidParameterException ipe) { |
| 122 | throw new InvalidAlgorithmParameterException(ipe.getMessage()); |
Valerie Peng | 2f4af22 | 2013-10-08 11:07:31 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Xue-Lei Andrew Fan | 7f30460 | 2016-04-15 11:09:18 +0000 | [diff] [blame] | 125 | this.random = random; |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Generates the parameters. |
| 130 | * |
| 131 | * @return the new AlgorithmParameters object |
| 132 | */ |
Xue-Lei Andrew Fan | 7f30460 | 2016-04-15 11:09:18 +0000 | [diff] [blame] | 133 | @Override |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 134 | protected AlgorithmParameters engineGenerateParameters() { |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 135 | |
Xue-Lei Andrew Fan | 7f30460 | 2016-04-15 11:09:18 +0000 | [diff] [blame] | 136 | if (random == null) { |
| 137 | random = SunJCE.getRandom(); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Xue-Lei Andrew Fan | 7f30460 | 2016-04-15 11:09:18 +0000 | [diff] [blame] | 140 | BigInteger paramP = null; |
| 141 | BigInteger paramG = null; |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 142 | try { |
Xue-Lei Andrew Fan | 7f30460 | 2016-04-15 11:09:18 +0000 | [diff] [blame] | 143 | AlgorithmParameterGenerator dsaParamGen = |
| 144 | AlgorithmParameterGenerator.getInstance("DSA"); |
| 145 | dsaParamGen.init(primeSize, random); |
| 146 | AlgorithmParameters dsaParams = dsaParamGen.generateParameters(); |
| 147 | DSAParameterSpec dsaParamSpec = |
| 148 | dsaParams.getParameterSpec(DSAParameterSpec.class); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 149 | |
| 150 | DHParameterSpec dhParamSpec; |
| 151 | if (this.exponentSize > 0) { |
| 152 | dhParamSpec = new DHParameterSpec(dsaParamSpec.getP(), |
| 153 | dsaParamSpec.getG(), |
| 154 | this.exponentSize); |
| 155 | } else { |
| 156 | dhParamSpec = new DHParameterSpec(dsaParamSpec.getP(), |
| 157 | dsaParamSpec.getG()); |
| 158 | } |
Xue-Lei Andrew Fan | 7f30460 | 2016-04-15 11:09:18 +0000 | [diff] [blame] | 159 | AlgorithmParameters algParams = |
| 160 | AlgorithmParameters.getInstance("DH", SunJCE.getInstance()); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 161 | algParams.init(dhParamSpec); |
Xue-Lei Andrew Fan | 7f30460 | 2016-04-15 11:09:18 +0000 | [diff] [blame] | 162 | |
| 163 | return algParams; |
| 164 | } catch (Exception ex) { |
| 165 | throw new ProviderException("Unexpected exception", ex); |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 166 | } |
J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 167 | } |
| 168 | } |