J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2007 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package com.sun.crypto.provider; |
| 27 | |
| 28 | import java.security.SecureRandom; |
| 29 | import java.security.InvalidParameterException; |
| 30 | import java.security.InvalidAlgorithmParameterException; |
| 31 | import java.security.InvalidKeyException; |
| 32 | import java.security.spec.AlgorithmParameterSpec; |
| 33 | import javax.crypto.KeyGeneratorSpi; |
| 34 | import javax.crypto.SecretKey; |
| 35 | import javax.crypto.spec.DESKeySpec; |
| 36 | |
| 37 | /** |
| 38 | * This class generates a DES key. |
| 39 | * |
| 40 | * @author Jan Luehe |
| 41 | * |
| 42 | */ |
| 43 | |
| 44 | public final class DESKeyGenerator extends KeyGeneratorSpi { |
| 45 | |
| 46 | private SecureRandom random = null; |
| 47 | |
| 48 | /** |
| 49 | * Verify the SunJCE provider in the constructor. |
| 50 | * |
| 51 | * @exception SecurityException if fails to verify |
| 52 | * its own integrity |
| 53 | */ |
| 54 | public DESKeyGenerator() { |
| 55 | if (!SunJCE.verifySelfIntegrity(this.getClass())) { |
| 56 | throw new SecurityException("The SunJCE provider may have " + |
| 57 | "been tampered."); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Initializes this key generator. |
| 63 | * |
| 64 | * @param random the source of randomness for this generator |
| 65 | */ |
| 66 | protected void engineInit(SecureRandom random) { |
| 67 | this.random = random; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Initializes this key generator with the specified parameter |
| 72 | * set and a user-provided source of randomness. |
| 73 | * |
| 74 | * @param params the key generation parameters |
| 75 | * @param random the source of randomness for this key generator |
| 76 | * |
| 77 | * @exception InvalidAlgorithmParameterException if <code>params</code> is |
| 78 | * inappropriate for this key generator |
| 79 | */ |
| 80 | protected void engineInit(AlgorithmParameterSpec params, |
| 81 | SecureRandom random) |
| 82 | throws InvalidAlgorithmParameterException { |
| 83 | throw new InvalidAlgorithmParameterException |
| 84 | ("DES key generation does not take any parameters"); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Initializes this key generator for a certain keysize, using the given |
| 89 | * source of randomness. |
| 90 | * |
| 91 | * @param keysize the keysize. This is an algorithm-specific |
| 92 | * metric specified in number of bits. |
| 93 | * @param random the source of randomness for this key generator |
| 94 | */ |
| 95 | protected void engineInit(int keysize, SecureRandom random) { |
| 96 | if (keysize != 56) { |
| 97 | throw new InvalidParameterException("Wrong keysize: must " |
| 98 | + "be equal to 56"); |
| 99 | } |
| 100 | this.engineInit(random); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Generates the DES key. |
| 105 | * |
| 106 | * @return the new DES key |
| 107 | */ |
| 108 | protected SecretKey engineGenerateKey() { |
| 109 | DESKey desKey = null; |
| 110 | |
| 111 | if (this.random == null) { |
| 112 | this.random = SunJCE.RANDOM; |
| 113 | } |
| 114 | |
| 115 | try { |
| 116 | byte[] key = new byte[DESKeySpec.DES_KEY_LEN]; |
| 117 | do { |
| 118 | this.random.nextBytes(key); |
| 119 | setParityBit(key, 0); |
| 120 | } while (DESKeySpec.isWeak(key, 0)); |
| 121 | desKey = new DESKey(key); |
| 122 | } catch (InvalidKeyException e) { |
| 123 | // this is never thrown |
| 124 | } |
| 125 | |
| 126 | return desKey; |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Does parity adjustment, using bit in position 8 as the parity bit, |
| 131 | * for 8 key bytes, starting at <code>offset</code>. |
| 132 | * |
| 133 | * The 8 parity bits of a DES key are only used for sanity-checking |
| 134 | * of the key, to see if the key could actually be a key. If you check |
| 135 | * the parity of the quantity, and it winds up not having the correct |
| 136 | * parity, then you'll know something went wrong. |
| 137 | * |
| 138 | * A key that is not parity adjusted (e.g. e4e4e4e4e4e4e4e4) produces the |
| 139 | * same output as a key that is parity adjusted (e.g. e5e5e5e5e5e5e5e5), |
| 140 | * because it is the 56 bits of the DES key that are cryptographically |
| 141 | * significant/"effective" -- the other 8 bits are just used for parity |
| 142 | * checking. |
| 143 | */ |
| 144 | static void setParityBit(byte[] key, int offset) { |
| 145 | if (key == null) |
| 146 | return; |
| 147 | |
| 148 | for (int i = 0; i < DESKeySpec.DES_KEY_LEN; i++) { |
| 149 | int b = key[offset] & 0xfe; |
| 150 | b |= (Integer.bitCount(b) & 1) ^ 1; |
| 151 | key[offset++] = (byte)b; |
| 152 | } |
| 153 | } |
| 154 | } |