Showing the changes to java/pkg/com/sun/crypto/provider/KeyGeneratorCore.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,221 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2003, 2009, 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.security.*; | ||
| 29 | import java.security.spec.AlgorithmParameterSpec; | ||
| 30 | |||
| 31 | import javax.crypto.*; | ||
| 32 | import javax.crypto.spec.SecretKeySpec; | ||
| 33 | |||
| 34 | /** | ||
| 35 | * KeyGeneratore core implementation and individual key generator | ||
| 36 | * implementations. Because of US export regulations, we cannot use | ||
| 37 | * subclassing to achieve code sharing between the key generator | ||
| 38 | * implementations for our various algorithms. Instead, we have the | ||
| 39 | * core implementation in this KeyGeneratorCore class, which is used | ||
| 40 | * by the individual implementations. See those further down in this | ||
| 41 | * file. | ||
| 42 | * | ||
| 43 | * @since 1.5 | ||
| 44 | * @author Andreas Sterbenz | ||
| 45 | */ | ||
| 46 | final class KeyGeneratorCore { | ||
| 47 | |||
| 48 | // algorithm name to use for the generator keys | ||
| 49 | private final String name; | ||
| 50 | |||
| 51 | // default key size in bits | ||
| 52 | private final int defaultKeySize; | ||
| 53 | |||
| 54 | // current key size in bits | ||
| 55 | private int keySize; | ||
| 56 | |||
| 57 | // PRNG to use | ||
| 58 | private SecureRandom random; | ||
| 59 | |||
| 60 | /** | ||
| 61 | * Construct a new KeyGeneratorCore object with the specified name | ||
| 62 | * and defaultKeySize. Initialize to default key size in case the | ||
| 63 | * application does not call any of the init() methods. | ||
| 64 | */ | ||
| 65 | KeyGeneratorCore(String name, int defaultKeySize) { | ||
| 66 | this.name = name; | ||
| 67 | this.defaultKeySize = defaultKeySize; | ||
| 68 | implInit(null); | ||
| 69 | } | ||
| 70 | |||
| 71 | // implementation for engineInit(), see JCE doc | ||
| 72 | // reset keySize to default | ||
| 73 | void implInit(SecureRandom random) { | ||
| 74 | this.keySize = defaultKeySize; | ||
| 75 | this.random = random; | ||
| 76 | } | ||
| 77 | |||
| 78 | // implementation for engineInit(), see JCE doc | ||
| 79 | // we do not support any parameters | ||
| 80 | void implInit(AlgorithmParameterSpec params, SecureRandom random) | ||
| 81 | throws InvalidAlgorithmParameterException { | ||
| 82 | throw new InvalidAlgorithmParameterException | ||
| 83 | (name + " key generation does not take any parameters"); | ||
| 84 | } | ||
| 85 | |||
| 86 | // implementation for engineInit(), see JCE doc | ||
| 87 | // we enforce a general 40 bit minimum key size for security | ||
| 88 | void implInit(int keysize, SecureRandom random) { | ||
| 89 | if (keysize < 40) { | ||
| 90 | throw new InvalidParameterException | ||
| 91 | ("Key length must be at least 40 bits"); | ||
| 92 | } | ||
| 93 | this.keySize = keysize; | ||
| 94 | this.random = random; | ||
| 95 | } | ||
| 96 | |||
| 97 | // implementation for engineInit(), see JCE doc | ||
| 98 | // generate the key | ||
| 99 | SecretKey implGenerateKey() { | ||
| 100 | if (random == null) { | ||
| 101 | random = SunJCE.RANDOM; | ||
| 102 | } | ||
| 103 | byte[] b = new byte[(keySize + 7) >> 3]; | ||
| 104 | random.nextBytes(b); | ||
| 105 | return new SecretKeySpec(b, name); | ||
| 106 | } | ||
| 107 | |||
| 108 | // nested static class for the HmacSHA256 key generator | ||
| 109 | public static final class HmacSHA256KG extends KeyGeneratorSpi { | ||
| 110 | private final KeyGeneratorCore core; | ||
| 111 | public HmacSHA256KG() { | ||
| 112 | core = new KeyGeneratorCore("HmacSHA256", 256); | ||
| 113 | } | ||
| 114 | protected void engineInit(SecureRandom random) { | ||
| 115 | core.implInit(random); | ||
| 116 | } | ||
| 117 | protected void engineInit(AlgorithmParameterSpec params, | ||
| 118 | SecureRandom random) throws InvalidAlgorithmParameterException { | ||
| 119 | core.implInit(params, random); | ||
| 120 | } | ||
| 121 | protected void engineInit(int keySize, SecureRandom random) { | ||
| 122 | core.implInit(keySize, random); | ||
| 123 | } | ||
| 124 | protected SecretKey engineGenerateKey() { | ||
| 125 | return core.implGenerateKey(); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | // nested static class for the HmacSHA384 key generator | ||
| 130 | public static final class HmacSHA384KG extends KeyGeneratorSpi { | ||
| 131 | private final KeyGeneratorCore core; | ||
| 132 | public HmacSHA384KG() { | ||
| 133 | core = new KeyGeneratorCore("HmacSHA384", 384); | ||
| 134 | } | ||
| 135 | protected void engineInit(SecureRandom random) { | ||
| 136 | core.implInit(random); | ||
| 137 | } | ||
| 138 | protected void engineInit(AlgorithmParameterSpec params, | ||
| 139 | SecureRandom random) throws InvalidAlgorithmParameterException { | ||
| 140 | core.implInit(params, random); | ||
| 141 | } | ||
| 142 | protected void engineInit(int keySize, SecureRandom random) { | ||
| 143 | core.implInit(keySize, random); | ||
| 144 | } | ||
| 145 | protected SecretKey engineGenerateKey() { | ||
| 146 | return core.implGenerateKey(); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | // nested static class for the HmacSHA384 key generator | ||
| 151 | public static final class HmacSHA512KG extends KeyGeneratorSpi { | ||
| 152 | private final KeyGeneratorCore core; | ||
| 153 | public HmacSHA512KG() { | ||
| 154 | core = new KeyGeneratorCore("HmacSHA512", 512); | ||
| 155 | } | ||
| 156 | protected void engineInit(SecureRandom random) { | ||
| 157 | core.implInit(random); | ||
| 158 | } | ||
| 159 | protected void engineInit(AlgorithmParameterSpec params, | ||
| 160 | SecureRandom random) throws InvalidAlgorithmParameterException { | ||
| 161 | core.implInit(params, random); | ||
| 162 | } | ||
| 163 | protected void engineInit(int keySize, SecureRandom random) { | ||
| 164 | core.implInit(keySize, random); | ||
| 165 | } | ||
| 166 | protected SecretKey engineGenerateKey() { | ||
| 167 | return core.implGenerateKey(); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | // nested static class for the RC2 key generator | ||
| 172 | public static final class RC2KeyGenerator extends KeyGeneratorSpi { | ||
| 173 | private final KeyGeneratorCore core; | ||
| 174 | public RC2KeyGenerator() { | ||
| 175 | core = new KeyGeneratorCore("RC2", 128); | ||
| 176 | } | ||
| 177 | protected void engineInit(SecureRandom random) { | ||
| 178 | core.implInit(random); | ||
| 179 | } | ||
| 180 | protected void engineInit(AlgorithmParameterSpec params, | ||
| 181 | SecureRandom random) throws InvalidAlgorithmParameterException { | ||
| 182 | core.implInit(params, random); | ||
| 183 | } | ||
| 184 | protected void engineInit(int keySize, SecureRandom random) { | ||
| 185 | if ((keySize < 40) || (keySize > 1024)) { | ||
| 186 | throw new InvalidParameterException("Key length for RC2" | ||
| 187 | + " must be between 40 and 1024 bits"); | ||
| 188 | } | ||
| 189 | core.implInit(keySize, random); | ||
| 190 | } | ||
| 191 | protected SecretKey engineGenerateKey() { | ||
| 192 | return core.implGenerateKey(); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | // nested static class for the ARCFOUR (RC4) key generator | ||
| 197 | public static final class ARCFOURKeyGenerator extends KeyGeneratorSpi { | ||
| 198 | private final KeyGeneratorCore core; | ||
| 199 | public ARCFOURKeyGenerator() { | ||
| 200 | core = new KeyGeneratorCore("ARCFOUR", 128); | ||
| 201 | } | ||
| 202 | protected void engineInit(SecureRandom random) { | ||
| 203 | core.implInit(random); | ||
| 204 | } | ||
| 205 | protected void engineInit(AlgorithmParameterSpec params, | ||
| 206 | SecureRandom random) throws InvalidAlgorithmParameterException { | ||
| 207 | core.implInit(params, random); | ||
| 208 | } | ||
| 209 | protected void engineInit(int keySize, SecureRandom random) { | ||
| 210 | if ((keySize < 40) || (keySize > 1024)) { | ||
| 211 | throw new InvalidParameterException("Key length for ARCFOUR" | ||
| 212 | + " must be between 40 and 1024 bits"); | ||
| 213 | } | ||
| 214 | core.implInit(keySize, random); | ||
| 215 | } | ||
| 216 | protected SecretKey engineGenerateKey() { | ||
| 217 | return core.implGenerateKey(); | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | } | ||