J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1999-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.spec.AlgorithmParameterSpec; |
| 32 | import javax.crypto.KeyGeneratorSpi; |
| 33 | import javax.crypto.SecretKey; |
| 34 | import javax.crypto.spec.SecretKeySpec; |
| 35 | |
| 36 | /** |
| 37 | * This class generates a secret key for use with the HMAC-SHA1 algorithm. |
| 38 | * |
| 39 | * @author Jan Luehe |
| 40 | * |
| 41 | */ |
| 42 | |
| 43 | public final class HmacSHA1KeyGenerator extends KeyGeneratorSpi { |
| 44 | |
| 45 | private SecureRandom random = null; |
| 46 | private int keysize = 64; // default keysize (in number of bytes) |
| 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 HmacSHA1KeyGenerator() { |
| 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 | { |
| 84 | throw new InvalidAlgorithmParameterException |
| 85 | ("HMAC-SHA1 key generation does not take any parameters"); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Initializes this key generator for a certain keysize, using the given |
| 90 | * source of randomness. |
| 91 | * |
| 92 | * @param keysize the keysize. This is an algorithm-specific |
| 93 | * metric specified in number of bits. |
| 94 | * @param random the source of randomness for this key generator |
| 95 | */ |
| 96 | protected void engineInit(int keysize, SecureRandom random) { |
| 97 | this.keysize = (keysize+7) / 8; |
| 98 | this.engineInit(random); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Generates an HMAC-SHA1 key. |
| 103 | * |
| 104 | * @return the new HMAC-SHA1 key |
| 105 | */ |
| 106 | protected SecretKey engineGenerateKey() { |
| 107 | if (this.random == null) { |
| 108 | this.random = SunJCE.RANDOM; |
| 109 | } |
| 110 | |
| 111 | byte[] keyBytes = new byte[this.keysize]; |
| 112 | this.random.nextBytes(keyBytes); |
| 113 | |
| 114 | return new SecretKeySpec(keyBytes, "HmacSHA1"); |
| 115 | } |
| 116 | } |