Showing the changes to java/pkg/com/sun/crypto/provider/DESedeKeyGenerator.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,145 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1997, 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 javax.crypto.KeyGeneratorSpi; | ||
| 29 | import javax.crypto.SecretKey; | ||
| 30 | import javax.crypto.spec.DESedeKeySpec; | ||
| 31 | import java.security.SecureRandom; | ||
| 32 | import java.security.InvalidParameterException; | ||
| 33 | import java.security.InvalidAlgorithmParameterException; | ||
| 34 | import java.security.InvalidKeyException; | ||
| 35 | import java.security.spec.AlgorithmParameterSpec; | ||
| 36 | |||
| 37 | /** | ||
| 38 | * This class generates a Triple DES key. | ||
| 39 | * | ||
| 40 | * @author Jan Luehe | ||
| 41 | * | ||
| 42 | */ | ||
| 43 | |||
| 44 | public final class DESedeKeyGenerator extends KeyGeneratorSpi { | ||
| 45 | |||
| 46 | private SecureRandom random = null; | ||
| 47 | private int keysize = 168; | ||
| 48 | |||
| 49 | /** | ||
| 50 | * Empty constructor | ||
| 51 | */ | ||
| 52 | public DESedeKeyGenerator() { | ||
| 53 | } | ||
| 54 | |||
| 55 | /** | ||
| 56 | * Initializes this key generator. | ||
| 57 | * | ||
| 58 | * @param random the source of randomness for this generator | ||
| 59 | */ | ||
| 60 | protected void engineInit(SecureRandom random) { | ||
| 61 | this.random = random; | ||
| 62 | } | ||
| 63 | |||
| 64 | /** | ||
| 65 | * Initializes this key generator with the specified parameter | ||
| 66 | * set and a user-provided source of randomness. | ||
| 67 | * | ||
| 68 | * @param params the key generation parameters | ||
| 69 | * @param random the source of randomness for this key generator | ||
| 70 | * | ||
| 71 | * @exception InvalidAlgorithmParameterException if <code>params</code> is | ||
| 72 | * inappropriate for this key generator | ||
| 73 | */ | ||
| 74 | protected void engineInit(AlgorithmParameterSpec params, | ||
| 75 | SecureRandom random) | ||
| 76 | throws InvalidAlgorithmParameterException { | ||
| 77 | throw new InvalidAlgorithmParameterException | ||
| 78 | ("Triple DES key generation does not take any parameters"); | ||
| 79 | } | ||
| 80 | |||
| 81 | /** | ||
| 82 | * Initializes this key generator for a certain keysize, using the given | ||
| 83 | * source of randomness. | ||
| 84 | * | ||
| 85 | * @param keysize the keysize. This is an algorithm-specific | ||
| 86 | * metric specified in number of bits. A keysize with 112 bits of entropy | ||
| 87 | * corresponds to a Triple DES key with 2 intermediate keys, and a keysize | ||
| 88 | * with 168 bits of entropy corresponds to a Triple DES key with 3 | ||
| 89 | * intermediate keys. | ||
| 90 | * @param random the source of randomness for this key generator | ||
| 91 | */ | ||
| 92 | protected void engineInit(int keysize, SecureRandom random) { | ||
| 93 | if ((keysize != 112) && (keysize != 168)) { | ||
| 94 | throw new InvalidParameterException("Wrong keysize: must be " | ||
| 95 | + "equal to 112 or 168"); | ||
| 96 | } | ||
| 97 | this.keysize = keysize; | ||
| 98 | this.engineInit(random); | ||
| 99 | } | ||
| 100 | |||
| 101 | /** | ||
| 102 | * Generates the Triple DES key. | ||
| 103 | * | ||
| 104 | * @return the new Triple DES key | ||
| 105 | */ | ||
| 106 | protected SecretKey engineGenerateKey() { | ||
| 107 | if (this.random == null) { | ||
| 108 | this.random = SunJCE.RANDOM; | ||
| 109 | } | ||
| 110 | |||
| 111 | byte[] rawkey = new byte[DESedeKeySpec.DES_EDE_KEY_LEN]; | ||
| 112 | |||
| 113 | if (keysize == 168) { | ||
| 114 | // 3 intermediate keys | ||
| 115 | this.random.nextBytes(rawkey); | ||
| 116 | |||
| 117 | // Do parity adjustment for each intermediate key | ||
| 118 | DESKeyGenerator.setParityBit(rawkey, 0); | ||
| 119 | DESKeyGenerator.setParityBit(rawkey, 8); | ||
| 120 | DESKeyGenerator.setParityBit(rawkey, 16); | ||
| 121 | } else { | ||
| 122 | // 2 intermediate keys | ||
| 123 | byte[] tmpkey = new byte[16]; | ||
| 124 | this.random.nextBytes(tmpkey); | ||
| 125 | DESKeyGenerator.setParityBit(tmpkey, 0); | ||
| 126 | DESKeyGenerator.setParityBit(tmpkey, 8); | ||
| 127 | System.arraycopy(tmpkey, 0, rawkey, 0, tmpkey.length); | ||
| 128 | // Copy the first 8 bytes into the last | ||
| 129 | System.arraycopy(tmpkey, 0, rawkey, 16, 8); | ||
| 130 | java.util.Arrays.fill(tmpkey, (byte)0x00); | ||
| 131 | } | ||
| 132 | |||
| 133 | DESedeKey desEdeKey = null; | ||
| 134 | try { | ||
| 135 | desEdeKey = new DESedeKey(rawkey); | ||
| 136 | } catch (InvalidKeyException ike) { | ||
| 137 | // this never happens | ||
| 138 | throw new RuntimeException(ike.getMessage()); | ||
| 139 | } | ||
| 140 | |||
| 141 | java.util.Arrays.fill(rawkey, (byte)0x00); | ||
| 142 | |||
| 143 | return desEdeKey; | ||
| 144 | } | ||
| 145 | } | ||