Showing the changes to java/pkg/com/sun/crypto/provider/DHParameterGenerator.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,171 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved. | ||
| 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 | ||
| 7 | * published by the Free Software Foundation. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 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 | * | ||
| 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. | ||
| 24 | */ | ||
| 25 | |||
| 26 | package com.sun.crypto.provider; | ||
| 27 | |||
| 28 | import java.math.BigInteger; | ||
| 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. | ||
| 41 | * The size of the prime modulus defaults to 1024 bits. | ||
| 42 | * | ||
| 43 | * @author Jan Luehe | ||
| 44 | * | ||
| 45 | * | ||
| 46 | * @see java.security.AlgorithmParameters | ||
| 47 | * @see java.security.spec.AlgorithmParameterSpec | ||
| 48 | * @see DHParameters | ||
| 49 | */ | ||
| 50 | public final class DHParameterGenerator | ||
| 51 | extends AlgorithmParameterGeneratorSpi { | ||
| 52 | |||
| 53 | // The size in bits of the prime modulus | ||
| 54 | private int primeSize = 1024; | ||
| 55 | |||
| 56 | // The size in bits of the random exponent (private value) | ||
| 57 | private int exponentSize = 0; | ||
| 58 | |||
| 59 | // The source of randomness | ||
| 60 | private SecureRandom random = null; | ||
| 61 | |||
| 62 | /** | ||
| 63 | * Initializes this parameter generator for a certain keysize | ||
| 64 | * and source of randomness. | ||
| 65 | * The keysize is specified as the size in bits of the prime modulus. | ||
| 66 | * | ||
| 67 | * @param keysize the keysize (size of prime modulus) in bits | ||
| 68 | * @param random the source of randomness | ||
| 69 | */ | ||
| 70 | protected void engineInit(int keysize, SecureRandom random) { | ||
| 71 | if ((keysize < 512) || (keysize > 1024) || (keysize % 64 != 0)) { | ||
| 72 | throw new InvalidParameterException("Keysize must be multiple " | ||
| 73 | + "of 64, and can only range " | ||
| 74 | + "from 512 to 1024 " | ||
| 75 | + "(inclusive)"); | ||
| 76 | } | ||
| 77 | this.primeSize = keysize; | ||
| 78 | this.random = random; | ||
| 79 | } | ||
| 80 | |||
| 81 | /** | ||
| 82 | * Initializes this parameter generator with a set of parameter | ||
| 83 | * generation values, which specify the size of the prime modulus and | ||
| 84 | * the size of the random exponent, both in bits. | ||
| 85 | * | ||
| 86 | * @param params the set of parameter generation values | ||
| 87 | * @param random the source of randomness | ||
| 88 | * | ||
| 89 | * @exception InvalidAlgorithmParameterException if the given parameter | ||
| 90 | * generation values are inappropriate for this parameter generator | ||
| 91 | */ | ||
| 92 | protected void engineInit(AlgorithmParameterSpec genParamSpec, | ||
| 93 | SecureRandom random) | ||
| 94 | throws InvalidAlgorithmParameterException { | ||
| 95 | if (!(genParamSpec instanceof DHGenParameterSpec)) { | ||
| 96 | throw new InvalidAlgorithmParameterException | ||
| 97 | ("Inappropriate parameter type"); | ||
| 98 | } | ||
| 99 | |||
| 100 | DHGenParameterSpec dhParamSpec = (DHGenParameterSpec)genParamSpec; | ||
| 101 | |||
| 102 | primeSize = dhParamSpec.getPrimeSize(); | ||
| 103 | if ((primeSize<512) || (primeSize>1024) || (primeSize%64 != 0)) { | ||
| 104 | throw new InvalidAlgorithmParameterException | ||
| 105 | ("Modulus size must be multiple of 64, and can only range " | ||
| 106 | + "from 512 to 1024 (inclusive)"); | ||
| 107 | } | ||
| 108 | |||
| 109 | exponentSize = dhParamSpec.getExponentSize(); | ||
| 110 | if (exponentSize <= 0) { | ||
| 111 | throw new InvalidAlgorithmParameterException | ||
| 112 | ("Exponent size must be greater than zero"); | ||
| 113 | } | ||
| 114 | |||
| 115 | // Require exponentSize < primeSize | ||
| 116 | if (exponentSize >= primeSize) { | ||
| 117 | throw new InvalidAlgorithmParameterException | ||
| 118 | ("Exponent size must be less than modulus size"); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | /** | ||
| 123 | * Generates the parameters. | ||
| 124 | * | ||
| 125 | * @return the new AlgorithmParameters object | ||
| 126 | */ | ||
| 127 | protected AlgorithmParameters engineGenerateParameters() { | ||
| 128 | AlgorithmParameters algParams = null; | ||
| 129 | |||
| 130 | if (this.exponentSize == 0) { | ||
| 131 | this.exponentSize = this.primeSize - 1; | ||
| 132 | } | ||
| 133 | |||
| 134 | if (this.random == null) | ||
| 135 | this.random = SunJCE.RANDOM; | ||
| 136 | |||
| 137 | try { | ||
| 138 | AlgorithmParameterGenerator paramGen; | ||
| 139 | DSAParameterSpec dsaParamSpec; | ||
| 140 | |||
| 141 | paramGen = AlgorithmParameterGenerator.getInstance("DSA"); | ||
| 142 | paramGen.init(this.primeSize, random); | ||
| 143 | algParams = paramGen.generateParameters(); | ||
| 144 | dsaParamSpec = (DSAParameterSpec) | ||
| 145 | algParams.getParameterSpec(DSAParameterSpec.class); | ||
| 146 | |||
| 147 | DHParameterSpec dhParamSpec; | ||
| 148 | if (this.exponentSize > 0) { | ||
| 149 | dhParamSpec = new DHParameterSpec(dsaParamSpec.getP(), | ||
| 150 | dsaParamSpec.getG(), | ||
| 151 | this.exponentSize); | ||
| 152 | } else { | ||
| 153 | dhParamSpec = new DHParameterSpec(dsaParamSpec.getP(), | ||
| 154 | dsaParamSpec.getG()); | ||
| 155 | } | ||
| 156 | algParams = AlgorithmParameters.getInstance("DH", "SunJCE"); | ||
| 157 | algParams.init(dhParamSpec); | ||
| 158 | } catch (InvalidParameterSpecException e) { | ||
| 159 | // this should never happen | ||
| 160 | throw new RuntimeException(e.getMessage()); | ||
| 161 | } catch (NoSuchAlgorithmException e) { | ||
| 162 | // this should never happen, because we provide it | ||
| 163 | throw new RuntimeException(e.getMessage()); | ||
| 164 | } catch (NoSuchProviderException e) { | ||
| 165 | // this should never happen, because we provide it | ||
| 166 | throw new RuntimeException(e.getMessage()); | ||
| 167 | } | ||
| 168 | |||
| 169 | return algParams; | ||
| 170 | } | ||
| 171 | } | ||