1/* 2 * Copyright (c) 1998, 2013, 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 26package com.sun.crypto.provider; 27 28import java.security.SecureRandom; 29import java.security.InvalidParameterException; 30import java.security.InvalidAlgorithmParameterException; 31import java.security.spec.AlgorithmParameterSpec; 32import javax.crypto.KeyGeneratorSpi; 33import javax.crypto.SecretKey; 34import javax.crypto.spec.SecretKeySpec; 35 36/** 37 * This class generates a secret key for use with the Blowfish algorithm. 38 * 39 * @author Jan Luehe 40 * 41 */ 42 43public final class BlowfishKeyGenerator extends KeyGeneratorSpi { 44 45 private SecureRandom random = null; 46 private int keysize = 16; // default keysize (in number of bytes) 47 48 /** 49 * Empty constructor 50 */ 51 public BlowfishKeyGenerator() { 52 } 53 54 /** 55 * Initializes this key generator. 56 * 57 * @param random the source of randomness for this generator 58 */ 59 protected void engineInit(SecureRandom random) { 60 this.random = random; 61 } 62 63 /** 64 * Initializes this key generator with the specified parameter 65 * set and a user-provided source of randomness. 66 * 67 * @param params the key generation parameters 68 * @param random the source of randomness for this key generator 69 * 70 * @exception InvalidAlgorithmParameterException if <code>params</code> is 71 * inappropriate for this key generator 72 */ 73 protected void engineInit(AlgorithmParameterSpec params, 74 SecureRandom random) 75 throws InvalidAlgorithmParameterException 76 { 77 throw new InvalidAlgorithmParameterException 78 ("Blowfish 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. 87 * @param random the source of randomness for this key generator 88 */ 89 protected void engineInit(int keysize, SecureRandom random) { 90 if (((keysize % 8) != 0) || (keysize < 32) || (keysize > 448)) { 91 throw new InvalidParameterException("Keysize must be " 92 + "multiple of 8, and can " 93 + "only range from 32 to 448 " 94 + "(inclusive)"); 95 } 96 this.keysize = keysize / 8; 97 this.engineInit(random); 98 } 99 100 /** 101 * Generates a Blowfish key. 102 * 103 * @return the new Blowfish key 104 */ 105 protected SecretKey engineGenerateKey() { 106 if (this.random == null) { 107 this.random = SunJCE.getRandom(); 108 } 109 110 byte[] keyBytes = new byte[this.keysize]; 111 this.random.nextBytes(keyBytes); 112 113 return new SecretKeySpec(keyBytes, "Blowfish"); 114 } 115} 116